Android应用程序在WIFI和3G(无代理)上运行,但不在3G上运行(如果分配了代理和端口)

时间:2012-09-27 08:46:53

标签: android sockets ssl ksoap2 3g

我面临一些奇怪的问题,当我切换到3G服务(使用代理)时,只有我的应用程序无法正常工作,但它在WIFI和3G(没有代理)方面工作得很好。

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

Proxy: 10.12.1.2
Port: 80

对于数据,我正在向我的网络服务器发送肥皂请求。

这是我的代码:

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 个答案:

答案 0 :(得分:6)

你可能已经太晚了,但是我希望这会有助于未来的人像我一样到达这里,对同样的问题感到疯狂。

这是一个Android错误。一切都适用于WiFi,但它在3G上崩溃。它发生在4.1上,但在4.2.2上没有问题。

要解决这个问题,你必须修改文件 org.ksoap2.transport.HttpTransportSE (我扩展了一个新文件,并将其称为MyHttpTransportSE,因为我使用的是ksoap2程序集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-

答案 1 :(得分:0)

正如Ender Muab'Dib所说,您必须将“连接”属性设置为“关闭”。但是没有必要扩展HttpTransportSE。根据代码的编写方式,可能更容易将第三个参数传递给作为参数列表的“调用”函数。

ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
transport.call(soapAction, envelope, headerPropertyArrayList);