DataSnap return empty dataset

大城市里の小女人 提交于 2019-12-24 13:20:11

问题


After more than a week's trial, I feel very depressed with DataSnap.

Today I again encountered a problem,

My Android(Java) client need to get a dataset from the DataSnap Server(XE6),but the server return a empty result:

{"result":[{"table":[]}]}

Here is the server code:

function TServerMethods1.GetDataset(cmdstr: string): TDBXReader;
var
  CMD: TDBXCommand;
begin
  SQLConnection1.Open;
  CMD := SQLConnection1.DBXConnection.CreateCommand;
  CMD.CommandType := TDBXCommandTypes.DbxSQL;
  CMD.Text := cmdstr;
  if not CMD.IsPrepared then
    CMD.Prepare;

  Result := CMD.ExecuteQuery;

  CMD.Free;  // If this line is commented out, it will memory leaks and return an exception.
end;

[Update-----------------]

Today I changed db access components to FireDAC:

{$METHODINFO ON}
  TServerMethods1 = class(TDataModule)
    Sqlite_demoConnection: TFDConnection;
    FDQuery1: TFDQuery;
    FDGUIxWaitCursor1: TFDGUIxWaitCursor;
    FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink;
  private
    { Private declarations }
  public
    { Public declarations }
    function EchoString(Value: string): string;
    function ReverseString(Value: string): string;
    function GetDataSet: TDataset;
  end;
{$METHODINFO OFF}


function TServerMethods1.GetDataSet: TDataset;
begin
  FDQuery1.SQL.Clear;
  FDQuery1.SQL.Text := 'select EmployeeID, LastName, FirstName, BirthDate from Employees';
  FDQuery1.Open();
  Result := FDQuery1;
end;

Delphi client can get the result and parse it. But Java client(Android) can't parse the result:

DSRESTConnection aConn = new DSRESTConnection();
DSProxy.TServerMethods1 aProxy = new DSProxy.TServerMethods1(conn);
aConn.setHost("192.168.0.240");
aConn.setPort(8080);
aConn.setProtocol("http");
try {
    TDataSet rd = aProxy.GetDataSet();

    int FieldCount = rd.getColumns().size();
    Log.i("FieldCount:", String.valueOf(FieldCount));  // get 4

    //rd.reset();
    while (rd.next())  // rd.next() get exception and return false
    {  
        Log.i("EmployeeID", rd.getValue("EmployeeID").GetAsString());
        Log.i("LastName", rd.getValue("LastName").GetAsString());

    }
} catch (DBXException e) {
    e.printStackTrace();
}

Update 2-------------------

OK, I can got the result. Thank you MartynA and J__.

and I found why rd.next() return false:

The dataset which return from server, include a datetime field. DataSnap transform it to json:

"BirthDate":["1948-12-08.0","1952-02-19.0","1963-08-30.0"]

Every date value append with a ".0" tail. So, Java can't transform back to Date type:

com.embarcadero.javaandroid.DBXException: Unparseable date: "1948-12-08.0" (at offset 10)

Then, I add a new server method to test return TDateTime type:

function TServerMethods1.GetDateTime: TDateTime;
begin
  Result := int(Now);
end;

DataSnap also put it into the style below:

{"result":["2014-06-27.0"]}

Ooh, My god. How to fix this? on Server side? on Client side?

来源:https://stackoverflow.com/questions/24428075/datasnap-return-empty-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!