我想使用带有Android的Delphi服务器作为客户端。但是我遇到了com.embarcadero.javaandroid.DBXException
错误。
我已经在Android清单中请求了Internet
权限,应用程序和服务器上的IP和端口相同,并且服务器正在工作,因为如果在资源管理器中执行它,就会看到响应。 / p>
如果我将Android设备与网络断开连接,则会遇到相同的错误,因此我认为问题出在Android上,但我不知道为什么会这样。
我使用本教程:
Android apps connected to Delphi DataSnap Server
这是我的主班:
//This method gets the connection to the server
private DSRESTConnection getConnection() {
DSRESTConnection conn = new DSRESTConnection();
conn.setHost("192.168.2.106");
conn.setPort(Integer.valueOf(puerto.getText().toString()));
return conn;
}
//This method calls another which is supposed to return the same string
private void insert()
{
DSRESTConnection conn = getConnection();
DSProxy.TServerMethods1 sm = new DSProxy.TServerMethods1(conn);
try {
String re;
re = sm.EchoString(buscar.getText().toString());
Toast.makeText(getApplicationContext(), " Resultado" + re, Toast.LENGTH_LONG).show();
} catch (DBXException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), " ERROR "+e, Toast.LENGTH_LONG).show();
}
这是我正在调用的方法:
package com.embarcadero.javaandroid;
public class DSProxy {
public static String valor;
public static class TServerMethods1 extends DSAdmin {
public TServerMethods1(DSRESTConnection Connection) {
super(Connection);
}
private DSRESTParameterMetaData[] TServerMethods1_EchoString_Metadata;
private DSRESTParameterMetaData[] get_TServerMethods1_EchoString_Metadata() {
if (TServerMethods1_EchoString_Metadata == null) {
TServerMethods1_EchoString_Metadata = new DSRESTParameterMetaData[]{
new DSRESTParameterMetaData("Value", DSRESTParamDirection.Input, DBXDataTypes.WideStringType, "string"),
new DSRESTParameterMetaData("", DSRESTParamDirection.ReturnValue, DBXDataTypes.WideStringType, "string"),
};
}
return TServerMethods1_EchoString_Metadata;
}
/**
* @param Value [in] - Type on server: string
* @return result - Type on server: string
*/
public String EchoString(String Value) throws DBXException {
DSRESTCommand cmd = getConnection().CreateCommand();
cmd.setRequestType(DSHTTPRequestType.GET);
cmd.setText("TServerMethods1.EchoString");
cmd.prepare(get_TServerMethods1_EchoString_Metadata());
cmd.getParameter(0).getValue().SetAsString(Value);
getConnection().execute(cmd);
return cmd.getParameter(1).getValue().GetAsString();
}
}
}
这是我从调试器得到的:
W/System.err: com.embarcadero.javaandroid.DBXException W/System.err: at com.embarcadero.javaandroid.DSRESTConnection.execute(DSRESTConnection.java:336) at com.embarcadero.javaandroid.DSProxy$TServerMethods1.EchoString(DSProxy.java:41) at com.expertos.conexionconservidor.MainActivity.insert(MainActivity.java:58) at com.expertos.conexionconservidor.MainActivity.access$000(MainActivity.java:15) at com.expertos.conexionconservidor.MainActivity$1.onClick(MainActivity.java:36) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22433) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6130) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
最后,此方法会产生错误:
public void execute(DSRESTCommand command) throws DBXException {
HttpClient client = null;
HttpUriRequest method = CreateRequest(command);
try {
if (isHttps) {
if (SSLFactory != null) {
client = SSLFactory.getHttpClient(connectionTimeout,
communicationTimeout);
} else {
throw new DBXException("Cannot create https connection");
}
} else
client = getHttpClient(); // fallback to the http one
HttpResponse response = client.execute(method);
setSessionIdentifier(response);
throwExceptionIfNeeded(response);
if (isThereOnlyOneStreamInOutput(command.getParameters())) {
InputStream inputstream = response.getEntity().getContent();
byte[] b1 = DBXTools.streamToByteArray(inputstream);
TStream is = new TStream(b1);
for (DSRESTParameter param : command.getParameters()) {
if ((param.Direction == DSRESTParamDirection.ReturnValue)
|| (param.Direction == DSRESTParamDirection.InputOutput)
|| (param.Direction == DSRESTParamDirection.Output)) {
if (param.TypeName.startsWith("TDBX")
&& param.TypeName.endsWith("Value")) {
param.getValue().GetAsDBXValue().SetAsStream(is);
} else {
param.getValue().SetAsStream(is);
}
break;
} // if
} // for
} else {
try {
String s = EntityUtils.toString(response.getEntity());
TJSONObject json = TJSONObject.Parse(s);
throwExceptionIfNeeded(json);
TJSONArray results = json.getJSONArray("result");
int returnParIndex = 0;
for (DSRESTParameter param : command.getParameters()) {
if ((param.Direction == DSRESTParamDirection.ReturnValue)
|| (param.Direction == DSRESTParamDirection.InputOutput)
|| (param.Direction == DSRESTParamDirection.Output)) {
DBXJSONTools.JSONtoDBX(results.get(returnParIndex),
param.getValue(), param.TypeName);
returnParIndex++;
} // if
} // for
} catch (DBXException e) {
throw new DBXException(e);
}
}
} catch (Exception e) {
throw new DBXException(e);
}
}