2012-07-13 49 views
3

我使用Android系統的查詢,使HTTP調用,我不斷收到隨機SSLExceptions,像這樣的:Android的查詢 - 隨機SSLExceptions

AQuery(7746): javax.net.ssl.SSLException: Read error: ssl=0x19d3c0: I/O error during system call, Connection reset by peer 
AQuery(7746): at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(Native Method) 
AQuery(7746): at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:801) 
AQuery(7746): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103) 
AQuery(7746): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191) 
AQuery(7746): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82) 
AQuery(7746): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174) 
AQuery(7746): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180) 
AQuery(7746): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235) 
AQuery(7746): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259) 
AQuery(7746): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279) 
AQuery(7746): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121) 
AQuery(7746): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428) 
AQuery(7746): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
AQuery(7746): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
AQuery(7746): at com.androidquery.callback.AbstractAjaxCallback.httpDo(AbstractAjaxCallback.java:1328) 
AQuery(7746): at com.androidquery.callback.AbstractAjaxCallback.httpGet(AbstractAjaxCallback.java:1207) 
AQuery(7746): at com.androidquery.callback.AbstractAjaxCallback.network(AbstractAjaxCallback.java:1133) 
AQuery(7746): at com.androidquery.callback.AbstractAjaxCallback.networkWork(AbstractAjaxCallback.java:986) 
AQuery(7746): at com.androidquery.callback.AbstractAjaxCallback.backgroundWork(AbstractAjaxCallback.java:933) 
AQuery(7746): at com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:894) 
AQuery(7746): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081) 
AQuery(7746): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574) 
AQuery(7746): at java.lang.Thread.run(Thread.java:1020) 

似乎沒有無緣無故爲他們的出現。當我撥打任何電話時,它們可能只有1/6發生。

這裏是我如何使用Android系統的查詢($):

//創建應用程序:

public void onCreate() 
{ 
    AQUtility.setExceptionHandler(new UncaughtExceptionHandler() { 
    public void uncaughtException(Thread thread, Throwable ex) 
    { 
     ex.printStackTrace(); 
    } 
    }); 

    AQUtility.setDebug(true); 

    AjaxCallback.setTransformer(new JsonTransformer()); 

    //set the max number of concurrent network connections, default is 4 
    AjaxCallback.setNetworkLimit(8); 

    //set the max number of icons (image width <= 50) to be cached in memory, default is 20 
    BitmapAjaxCallback.setIconCacheLimit(20); 

    //set the max number of images (image width > 50) to be cached in memory, default is 20 
    BitmapAjaxCallback.setCacheLimit(40); 

    //set the max size of an image to be cached in memory, default is 1600 pixels (ie. 400x400) 
    BitmapAjaxCallback.setPixelLimit(480 * 480); 

    //set the max size of the memory cache, default is 1M pixels (4MB) (2---) 
    BitmapAjaxCallback.setMaxPixelLimit(4000000); 

    super.onCreate(); 
} 

//用法 //注意:URI = // 07-13 17:26:08.040:W/AQuery(7746):得到:https://site.com/me/ticker/

$.auth(Security.getAuth(this)).ajax(uri, BaseModelListRequest.class, this, "loadAdapterObjectsCallback"); 

public void loadAdapterObjectsCallback(String uri, BaseModelListRequest requestData, AjaxStatus status) 
{ 
    setProgressBarIndeterminateVisibility(false); 
    setSupportProgressBarIndeterminateVisibility(false); 

    if(requestData != null && status.getCode() == 200) 
    { 
    // stuff 
    } 
} 

//這裏是GETAUTH:

public static synchronized BasicHandle getAuth(Context context) 
{ 
    return new BasicHandle("user", "pass"); 
} 

//及JSONTRANSFORMER(使用jaxson馬歇爾JSON):

public class JsonTransformer implements Transformer 
{ 
    public <T> T transform(String url, Class<T> type, String encoding, byte[] data, AjaxStatus status) 
    { 
    ObjectMapper mapper = new ObjectMapper(); 

    try 
    { 
     return mapper.readValue(new String(data), type); 
    } 
    catch(Exception e) 
    { 
     return null; 
    } 
    } 
} 

編輯:這發生在2.x和3.x,使用SSL。禁用SSL(https - > http)可以使其工作。

這裏是另一個例外,SSL:

07-13 22:03:41.454: W/AQuery(2517): javax.net.ssl.SSLException: Not trusted server certificate 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:360) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:92) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:321) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:140) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
07-13 22:03:41.454: W/AQuery(2517):  at com.androidquery.callback.AbstractAjaxCallback.httpDo(AbstractAjaxCallback.java:1328) 
07-13 22:03:41.454: W/AQuery(2517):  at com.androidquery.callback.AbstractAjaxCallback.httpGet(AbstractAjaxCallback.java:1207) 
07-13 22:03:41.454: W/AQuery(2517):  at com.androidquery.callback.AbstractAjaxCallback.network(AbstractAjaxCallback.java:1133) 
07-13 22:03:41.454: W/AQuery(2517):  at com.androidquery.callback.AbstractAjaxCallback.networkWork(AbstractAjaxCallback.java:986) 
07-13 22:03:41.454: W/AQuery(2517):  at com.androidquery.callback.AbstractAjaxCallback.backgroundWork(AbstractAjaxCallback.java:933) 
07-13 22:03:41.454: W/AQuery(2517):  at com.androidquery.callback.AbstractAjaxCallback.run(AbstractAjaxCallback.java:894) 
07-13 22:03:41.454: W/AQuery(2517):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
07-13 22:03:41.454: W/AQuery(2517):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
07-13 22:03:41.454: W/AQuery(2517):  at java.lang.Thread.run(Thread.java:1096) 
07-13 22:03:41.454: W/AQuery(2517): Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found. 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:168) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:355) 
07-13 22:03:41.454: W/AQuery(2517):  ... 17 more 
07-13 22:03:41.454: W/AQuery(2517): Caused by: java.security.cert.CertPathValidatorException: TrustAnchor for CertPath not found. 
07-13 22:03:41.454: W/AQuery(2517):  at org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi.engineValidate(PKIXCertPathValidatorSpi.java:149) 
07-13 22:03:41.454: W/AQuery(2517):  at java.security.cert.CertPathValidator.validate(CertPathValidator.java:211) 
07-13 22:03:41.454: W/AQuery(2517):  at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:164) 
07-13 22:03:41.454: W/AQuery(2517):  ... 18 more 

編輯2:

進一步檢查產率在Android 2.3.x版本設備發生錯誤。刪除協議中的「s」(即SSL)使一切正常工作。

我以前曾就此問題與RestTemplate(Spring的Android設備),並按照這篇文章的建議修復它:Self-signed SSL acceptance on Android

但是,我修改了源Android的查詢以同樣的方式,纔沒有解決問題。

+0

可能重複[Android HTTPS異常連接重置由同級](http://stackoverflow.com/questions/8472556/android-https-exception-connection-reset-by-peer) – 2014-04-07 17:25:41

回答

1

此問題已被問了幾次herehere。這種異常似乎是由服務器重置連接的結果。我不認爲除了優雅地處理異常並繼續前進之外,您可以在應用程序代碼中執行任何操作。如果服務器在您的控制之下,那麼值得檢查日誌和配置以查看導致連接重置的原因。您可能可以最大限度地減少或解決問題。