本人初接觸Android的開發。現在想試下在Android端連接用Delphi XE10.3開發的一個DataSnap Restful服務器,下列代碼能編譯通過,但運行時不能執行服務器的函式,轉到catch處執行,列印的錯誤資訊為Null。而在電腦中或手機上都能用瀏覽器正常執行這個Restful服務器。代碼如下(kotlin):請各位幫忙看看是什么原因,非常感謝!
package com.arkon.edittexttest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.embarcadero.javaandroid.DBXException
import com.embarcadero.javaandroid.DSProxy
import com.embarcadero.javaandroid.DSRESTConnection
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnReverse.setOnClickListener{
val conn: DSRESTConnection? = DSRESTConnection()
conn?.host = "172.16.9.12"
conn?.port = 8080
conn?.protocol = "http"
val serverProxy: DSProxy.TMyTestServerMethods? = DSProxy.TMyTestServerMethods(conn)
try {
var s: String? = serverProxy?.ReverseString(edtText.text.toString())
label2.text = s
} catch(e: DBXException){
e.printStackTrace()
label2.text = "發生錯誤,錯誤資訊:" + e.message
}
}
}
}
上面的是的測驗代碼,下面是用到的java包(Delphi 程式自動生成的),但因字數限制不能貼完全。
package com.embarcadero.javaandroid;
import java.util.Date;
public class DSProxy {
public static class TMyTestServerMethods extends DSAdmin {
public TMyTestServerMethods(DSRESTConnection Connection) {
super(Connection);
}
private DSRESTParameterMetaData[] TMyTestServerMethods_EchoString_Metadata;
private DSRESTParameterMetaData[] get_TMyTestServerMethods_EchoString_Metadata() {
if (TMyTestServerMethods_EchoString_Metadata == null) {
TMyTestServerMethods_EchoString_Metadata = new DSRESTParameterMetaData[]{
new DSRESTParameterMetaData("Value", DSRESTParamDirection.Input, DBXDataTypes.WideStringType, "string"),
new DSRESTParameterMetaData("", DSRESTParamDirection.ReturnValue, DBXDataTypes.WideStringType, "string"),
};
}
return TMyTestServerMethods_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("TMyTestServerMethods.EchoString");
cmd.prepare(get_TMyTestServerMethods_EchoString_Metadata());
cmd.getParameter(0).getValue().SetAsString(Value);
getConnection().execute(cmd);
return cmd.getParameter(1).getValue().GetAsString();
}
private DSRESTParameterMetaData[] TMyTestServerMethods_ReverseString_Metadata;
private DSRESTParameterMetaData[] get_TMyTestServerMethods_ReverseString_Metadata() {
if (TMyTestServerMethods_ReverseString_Metadata == null) {
TMyTestServerMethods_ReverseString_Metadata = new DSRESTParameterMetaData[]{
new DSRESTParameterMetaData("Value", DSRESTParamDirection.Input, DBXDataTypes.WideStringType, "string"),
new DSRESTParameterMetaData("", DSRESTParamDirection.ReturnValue, DBXDataTypes.WideStringType, "string"),
};
}
return TMyTestServerMethods_ReverseString_Metadata;
}
/**
* @param Value [in] - Type on server: string
* @return result - Type on server: string
*/
public String ReverseString(String Value) throws DBXException {
DSRESTCommand cmd = getConnection().CreateCommand();
cmd.setRequestType(DSHTTPRequestType.GET);
cmd.setText("TMyTestServerMethods.ReverseString");
cmd.prepare(get_TMyTestServerMethods_ReverseString_Metadata());
cmd.getParameter(0).getValue().SetAsString(Value);
getConnection().execute(cmd);
return cmd.getParameter(1).getValue().GetAsString();
}
}
}
package com.embarcadero.javaandroid;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.util.LinkedList;
import java.util.List;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
/**
* Allows you to manage and make REST requests based on the protocol, the target
* host , the context and other features. Supports authentication and
* authorization.
*
* Use the properties and methods of DSRESTConnection to: <br>
* - Set the general parameters of a connection as
* target host and port, context, protocol , URL path etc... <br>
* - Setting Username and Password for authentication
* and authorization. <br>
* - Execute a REST request from a {@link DSRESTCommand}
* . <br>
* - Rebuild and save the parameters contained in the
* response.
*
*/
public class DSRESTConnection {
public final static String TAG = "DataSnap";
/**
* This method returns the URL encoded string starting from the input string
* URL. The characters encoding is UTF-8.
*
*
* @param value
* the URL string to encode
* @return the URL string encoded
*/
private String encodeURIComponent(String value) {
try {
String encodedURI = URLEncoder.encode(value, "UTF-8");
encodedURI = encodedURI.replaceAll("\\+", "%20");
encodedURI = encodedURI.replaceAll("\\%21", "!");
encodedURI = encodedURI.replaceAll("\\%27", "'");
encodedURI = encodedURI.replaceAll("\\%28", "(");
encodedURI = encodedURI.replaceAll("\\%29", ")");
encodedURI = encodedURI.replaceAll("\\%7E", "~");
return encodedURI;
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* This method returns the URL encoded string starting from the input
* {@link DSRESTParameter}
*
* @param parameter
* the DSRESTParameter to encode
* @return the DSRESTParameter encoded
*/
private String encodeURIComponent(DSRESTParameter parameter) {
return encodeURIComponent(parameter.getValue().toString());
}
private int Port = 0;
private String UrlPath = "";
private String Host = "";
private String Protocol = "";
private String Context = "";
private String UserName = "";
private String Password = "";
private int connectionTimeout = 5000;
private int communicationTimeout = 0;
private String SessionID;
private long SessionIDExpires;
private DSRESTSSLFactory SSLFactory;
private boolean isHttps;
/**
* Class constructor
*/
public DSRESTConnection() {
super();
InitSSLFactory();
CloseSession();
}
/**
* Initialize the SSL factory that will manage HTTP and HTTPS connections
*
* @throws UnrecoverableKeyException
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
private void InitSSLFactory() {
if (SSLFactory == null)
try {
SSLFactory = new DSRESTSSLFactory(null);
} catch (Exception ex) {
// throw new DBXException(ex.getMessage());
// Later will use the http interface silently
}
}
/**
* Clone the current connection. The session is not cloned, so the cloned
* connection will not have the same session as its parent.
*
* @return the new DSRESTConnection
*/
public DSRESTConnection Clone() {
return Clone(false);
}
/**
* Clone the current connection. The session is optionally included in the
* clone.
*
* @param includeSession
* true to include session information in the new connection,
* false to exclude it.
*
* @return the new DSRESTConnection
*/
public DSRESTConnection Clone(boolean includeSession) {
DSRESTConnection connection = new DSRESTConnection();
connection.setHost(this.getHost());
connection.setPort(this.getPort());
connection.setProtocol(this.getProtocol());
connection.setUserName(this.getUserName());
connection.setPassword(this.getPassword());
connection.setUrlPath(this.getUrlPath());
connection.setCommunicationTimeout(this.getCommunicationTimeout());
connection.setConnectionTimeout(this.getConnectionTimeout());
if (includeSession) {
connection.setSessionID(this.getSessionID());
connection.SessionIDExpires = this.SessionIDExpires;
}
return connection;
}
/**
* Factory method to quick create a command.
*
* @return a new DSRESTCommand
*/
public DSRESTCommand CreateCommand() {
return new DSRESTCommand(this);
}
/**
* Collects all the informations contained in this object like the target
* host, the protocol; the more information contained in
* {@link DSRESTCommand} input parameter as the type of request, the method
* to call then to return the url to run
*
*
* @param the
* specific DSRESTCommand
* @return a requested url
*/
private String BuildRequestURL(DSRESTCommand command) {
String LPathPrefix = getUrlPath();
int LPort = getPort();
String LHost = getHost();
String LMethodName = command.getText();
String LProtocol = getProtocol();
if (!LProtocol.equals("https"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/135591.html
標籤:Android
上一篇:基于openvino 2019R3的INT8推理(inference)性能的深入研究 (一) MobilenetV2
下一篇:QT使用自定義控制元件的問題
