我有一个 Android 应用程序,我在其中使用 ksoap2 连接到 Web 服务,但有时我会从客户端收到错误。当我在我的设备上测试自己时,一切都很好,所以我不确定哪里可能出现问题。
我得到这些例外:
java.net.ConnectException: failed to connect to example.com/91.10.10.10 (port 443) after 1800000ms: isConnected failed: ECONNREFUSED (Connection refused)
java.net.ConnectException: failed to connect to example.com/91.10.10.10 (port 443) after 1800000ms: connect failed: ENETUNREACH (Network is unreachable)
java.net.SocketException: failed to connect to example.com/91.10.10.10 (port 443) after 1800000ms: isConnected failed: EHOSTUNREACH (No route to host)
或者
java.net.SocketException: Socket is closed
或者
javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x5cb6d638: I/O error during system call, Connection timed out
我使用这个函数来调用 webservice 函数:
private SoapObject makeCallToWebService(String soapAction, String method, PropertyInfo[] properties, boolean runInBackground) {
int retry = RETRY_COUNT;
int count = 0;
if (mShouldRetryWhenError == false) {
retry = 1;
}
while (count < retry) {
count++;
SoapObject request = new SoapObject(NAMESPACE, method);
if (properties != null) {
for (PropertyInfo property : properties) {
request.addProperty(property);
}
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);;
HttpsTransportSE transport = new HttpsTransportSE(HOST, PORT, FILE, TIMEOUT);
transport.debug = true;
transport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
try {
transport.call(NAMESPACE + "#" + soapAction, envelope);
SoapObject res = (SoapObject)envelope.bodyIn;
return res;
} catch (SocketTimeoutException e) {
if (count == retry) {
if (!runInBackground) {
handleError(method);
}
}
} catch (Exception e) {
Log.e(TAG, e.toString());
if (!runInBackground) {
handleError(null);
}
return null;
}
}
return null;
}