DataSnap return empty dataset

2019-08-12 02:48发布

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?

0条回答
登录 后发表回答