2012-09-27 48 views
6

我面臨一些奇怪的問題,只有我的應用程序在切換到3G服務時不工作(使用代理),但它在WIFI和3G(沒有代理)方面效果很好。我的星和(網絡運營商)給出在WIFI和3G上工作的Android應用(沒有代理),但沒有在3G上工作(如果分配了代理和端口)

默認代理和端口:

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; 
} 
} 

例外:

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) 

附加信息:所有其他應用程序正在運行3 G網絡(有/無代理設置)和在我的應用程序中,只有SOAP請求不起作用。

我試過所有可能的情況,但沒有運氣。請給我提供一些意見。

在此先感謝。

回答

6

也許你已經太晚了四次,但我希望這會有助於未來的人們像我一樣到達這裏,在同樣的問題上變得瘋狂。

這是一個Android bug。一切工作正常與WiFi,但它在3G上崩潰。它發生在4.1上,但在4.2.2上沒有問題。

爲了解決它,你必須修改文件org.ksoap2.transport.HttpTransportSE(我已經擴展了新的一個並把它稱爲MyHttpTransportSE因爲我使用kso​​ap2組裝罐子)。 只需重寫方法公共無效調用(字符串的soapAction,信封的SoapEnvelope)註釋掉行

//connection.setRequestProperty("Connection", "close"); 

當然,如果你使用SSL的工作,你需要從擴大自己HttpsTransportSE你的新的MyHttpTransportSE

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

這解決了我的問題。它應該被接受IMO。 –

相關問題