安卓应用在WIFI和3G网络下工作正常(没有代理),但在3G网络下无法工作(如果分配了代理和端口)。

6

我面临一个奇怪的问题,只有在我切换到3G服务时我的应用程序才不能工作(使用代理),但是在WIFI和3G(没有代理)中它表现得很好。

我的星和(网络提供商)提供的默认代理和端口:

Proxy: 10.12.1.2
Port: 80

对于数据,我正在向我的Web服务器发送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;
}
}

异常:

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请求无法工作。

我已经尝试了所有可能的情况,但没有运气。请给我一些建议。

提前致谢。

2个回答

6
也许对你来说已经晚了,但我希望这能帮到像我一样遇到同样问题的未来的人们。
这是一个 Android 的 bug。使用 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-

0
如Ender Muab'Dib所说,您需要将“Connection”属性设置为“close”。但是无需扩展HttpTransportSE。根据您的代码编写方式,可能更容易将第三个参数作为参数列表传递给“call”函数。
ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
transport.call(soapAction, envelope, headerPropertyArrayList);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接