Android的查询 - 随机SSLExceptions(Android-Query - Rando

2019-06-26 06:35发布

我使用的是Android系统的查询,使HTTP调用,我不断收到随机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();
}

// // USAGE注: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(弹簧安卓)这个问题,并按照这篇文章的建议修复它: 在Android上的自签名SSL验收

但是,我修改了源Android的查询以相同的方式,并没有解决问题。

Answer 1:

这个问题已经被问了几次, 在这里和这里 。 这个例外似乎是由服务器重置连接的结果。 我不认为有什么可以在除正常处理异常,并在移动应用程序代码做。 如果服务器是你的控制之下,将是值得的检查记录和配置,看看是什么原因造成的连接被重置。 您也许能够代替有减少或解决问题。



文章来源: Android-Query - Random SSLExceptions