我遇到了一些奇怪的问题,当我切换到 3G 服务(使用代理)时,只有我的应用程序不工作,但它在 WIFI 和 3G(没有代理)中运行良好。
我的 Starhub(网络提供商)提供的默认代理和端口:
Proxy: 10.12.1.2
Port: 80
对于数据,我正在向我的网络服务器发送 soap 请求。
这是我的代码:
public class SearchThread extends Thread {
private String mUrl;
private SoapSerializationEnvelope mEnvelop;
private Handler mHandler;
private String mSoapAction;
private KeepAliveHttpsTransportSE mTransport;
public SearchThread(String url) {
this.mUrl = url;
}
@Override
public void run() {
mEnvelop = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
mEnvelop.setOutputSoapObject(interfaceListener.getSoapObject(element));
mSoapAction = interfaceListener.getSoapAction(element);
try {
TrustManagerManipulator.allowAllSSL();
mTransport = new KeepAliveHttpsTransportSE(URLS.URL_MAIN, 443, mUrl, 60000);
mTransport.call(mSoapAction, mEnvelop);
if (this.isInterrupted()) {
Log.v(TAG,"Interrupted");
return;
}
recevedSoapResponse(mEnvelop.getResponse());
} catch (SocketException ex) {
Log.e("Error : ", "Error on soapPrimitiveData() " + ex.getMessage());
ex.printStackTrace();
} catch (IOException e) {
interfaceListener.recievedParsingResults(
PARSER.RESULT.CONNECTION_FAILED, element, mHandler, mView);
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
interfaceListener.recievedParsingResults(
PARSER.RESULT.INTERNAL_ERROR, element, mHandler, mView);
}
}
private void recevedSoapResponse(Object response) {
//Parsing XML here.
}
public class KeepAliveHttpsTransportSE extends HttpsTransportSE
{
private final String host;
private final int port;
private final String file;
private final int timeout;
private ServiceConnection serviceConnection;
public KeepAliveHttpsTransportSE (String host, int port, String file, int timeout) {
super(host, port, file, timeout);
this.host = host;
this.port = port;
this.file = file;
this.timeout = timeout;
}
//@Override
public ServiceConnection getServiceConnection() throws IOException
{
if (serviceConnection == null) {
serviceConnection = new HttpsServiceConnectionSE(host, port, file, timeout);
serviceConnection.setRequestProperty("Connection", "keep-alive");
}
return serviceConnection;
}
}
这是我的 SSL 代码:
public class TrustManagerManipulator implements X509TrustManager {
private static TrustManager[] trustManagers;
private static final X509Certificate[] acceptedIssuers = new X509Certificate[] {};
public boolean isClientTrusted(X509Certificate[] chain) {
return true;
}
public boolean isServerTrusted(X509Certificate[] chain) {
return true;
}
public static void allowAllSSL() {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = null;
if (trustManagers == null) {
trustManagers = new TrustManager[] { new TrustManagerManipulator() };
}
try {
context = SSLContext.getInstance("TLS");
context.init(null, trustManagers, new SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
HttpsURLConnection.setDefaultSSLSocketFactory(context
.getSocketFactory());
}
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return acceptedIssuers;
}
}
异常(exception):
09-27 12:21:03.295: W/System.err(8924): java.net.SocketException: Socket is closed
09-27 12:21:03.295: W/System.err(8924): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.checkOpen(OpenSSLSocketImpl.java:262)
09-27 12:21:03.295: W/System.err(8924): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:273)
09-27 12:21:03.295: W/System.err(8924): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:257)
09-27 12:21:03.295: W/System.err(8924): at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:210)
09-27 12:21:03.295: W/System.err(8924): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:477)
09-27 12:21:03.295: W/System.err(8924): at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:441)
09-27 12:21:03.295: W/System.err(8924): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
09-27 12:21:03.295: W/System.err(8924): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
09-27 12:21:03.295: W/System.err(8924): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
09-27 12:21:03.300: W/System.err(8924): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
09-27 12:21:03.300: W/System.err(8924): at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:280)
09-27 12:21:03.300: W/System.err(8924): at org.ksoap2.transport.HttpsServiceConnectionSE.openOutputStream(HttpsServiceConnectionSE.java:98)
09-27 12:21:03.300: W/System.err(8924): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:157)
09-27 12:21:03.300: W/System.err(8924): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
09-27 12:21:03.300: W/System.err(8924): at com.mobile.utils.parser.SearchThread.run(SearchThread.java:25)
附加信息:所有其他应用程序都在 3G 网络上运行(有/没有代理设置)并且在我的应用程序中只有 SOAP 请求不起作用。
我已经尝试了所有可能的情况,但没有成功。请提供一些意见。
提前致谢。
最佳答案
也许你们四个来不及了,但我希望这能帮助 future 像我一样来到这里,为同样的问题而疯狂的人。
这是一个 Android 错误。一切都适用于 WiFi,但它在 3G 上崩溃。它发生在我的 4.1 上,但在 4.2.2 上没有问题。
要解决它,您必须修改文件org.ksoap2.transport.HttpTransportSE(我扩展了一个新文件并将其命名为MyHttpTransportSE,因为我使用的是ksoap2 assembly jar)。 只需重写方法 public void call(String soapAction, SoapEnvelope envelope) 注释掉该行
//connection.setRequestProperty("Connection", "close");
当然,如果您使用 SSL,则需要从新的 MyHttpTransportSE 扩展您自己的 HttpsTransportSE。
HttpTransportSE 源代码:https://github.com/karlmdavis/ksoap2-android/blob/master/ksoap2-j2se/src/main/java/org/ksoap2/transport/HttpTransportSE.java?source=c
HttpsTransportSE 源代码:https://github.com/mosabua/ksoap2-android/blob/master/ksoap2-j2se/src/main/java/org/ksoap2/transport/HttpsTransportSE.java
此处有更多详细信息:https://groups.google.com/forum/#!searchin/ksoap2-android/closed/ksoap2-
关于Android 应用程序在 WIFI 和 3G(无代理)上工作,但在 3G 上不工作(如果分配了代理和端口),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12617451/