获取SSLContex实例时的Android ANR

时间:2016-08-19 15:37:10

标签: android android-anr-dialog

我正在开发一个Android应用程序。此应用程序使用ssl Web服务。但是,我有问题。我的应用程序启动后,我在logcat中看到以下警告:

int

如果我看到traces.txt:

08-19 18:29:31.283 26425-26431/ I/art: Thread[2,tid=26431,WaitingInMainSignalCatcherLoop,Thread*=0xb889e2a8,peer=0x22c004b8,"Signal Catcher"]: reacting to signal 3
08-19 18:29:31.283 26425-26431/ I/art: Mutator lock exclusive owner tid: 18446744073709551615
08-19 18:29:31.283 26425-26431/ I/art: ThreadList lock owner tid: 0
08-19 18:29:31.283 26425-26431/ I/art: ClassLinker classes lock owner tid: 0
08-19 18:29:31.283 26425-26431/ I/art: ClassLinker dex lock owner tid: -1
08-19 18:29:31.383 26425-26431/ I/art: Wrote stack traces to '/data/anr/traces.txt'

这是我的Http客户端工厂

DALVIK THREADS (12):
"main" prio=5 tid=1 Suspended
  | group="main" sCount=1 dsCount=0 obj=0x77770000 self=0xb8581a70
  | sysTid=8167 nice=0 cgrp=apps sched=0/0 handle=0xb6f56ec8
  | state=S schedstat=( 15220246856 372813867 1515 ) utm=919 stm=603 core=2 HZ=100
  | stack=0xbe6e3000-0xbe6e5000 stackSize=8MB
  | held mutexes=
  at java.lang.String.hashCode(String.java:836)
  at java.util.Collections.secondaryHash(Collections.java:3405)
  at java.util.HashMap.get(HashMap.java:300)
  at org.apache.harmony.security.fortress.Services.appendServiceLocked(Services.java:152)
  at org.apache.harmony.security.fortress.Services.initServiceInfo(Services.java:140)
  - locked <0x3906da66> (a java.lang.Class<org.apache.harmony.security.fortress.Services>)
  at org.apache.harmony.security.fortress.Services.getCacheVersion(Services.java:211)
  - locked <0x3906da66> (a java.lang.Class<org.apache.harmony.security.fortress.Services>)
  at org.apache.harmony.security.fortress.Engine.getServices(Engine.java:158)
  at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:137)
  at javax.net.ssl.SSLContext.getInstance(SSLContext.java:138)
  at com.app.client.MyHttpClientFactory.getNewURLConnection(MyHttpClientFactory.java:51)

修改

public static HttpsURLConnection getNewURLConnection() {

        String[] uriArr = Config.get().getUriArr();
        HttpsURLConnection conn = null;

        for (int i = 0; i < uriArr.length; i++) {

            TrustManager[] trustAllCerts = new TrustManager[] {
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                        }

                        @Override
                        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                        }

                        @Override
                        public X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }
                    }
            };

            try {

                SSLContext ssl = SSLContext.getInstance("SSL");
                ssl.init(null, trustAllCerts, new SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(ssl.getSocketFactory());

                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                });

                URL url = new URL(uriArr[i]);
                conn = (HttpsURLConnection) url.openConnection();

                conn.setRequestMethod("POST");

                conn.setDoInput(true);
                conn.setDoOutput(true);

                int timeoutSocket = 20000;
                conn.setReadTimeout(timeoutSocket);

                int timeoutConnection = 10000;
                conn.setConnectTimeout(timeoutConnection);
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty(HEADER_ACCEPT_ENCODING, ENCODING_GZIP);

            } catch (Exception e) {
                Log.e(TAG, "http client creation failed");
                conn = null;
            }

            if (conn != null)
                break;

        }

        return conn;
    }

ServerAccess类:

public class TaskService extends Service {

   @Override 
   public void onCreate() {
      super.onCreate()
   }

   private static class TaskHandler extends Handler {

      @Override
      public void handleMessage(Message msg) {
          MessageEnumProxy proxy = null;

          try {
            proxy = (MessageEnumProxy) msg.obj;
            CoreMessages whatMessage = (CoreMessages) proxy.getEnum();

            switch(whatMessage) {
               //bla bla
               case NEW_PICTURE_UPLOAD:
                    serverAccess.sendNewPicture();
               //bla bla
            }

          } catch (Exception e) {
          }
      }
   }

   private class TaskThread extends Thread implements MessageQueue.IdleHandler {
       public Handler mHandler = null;

       @Override
       public void run() {
          Looper.prepare();
          mHandler = new TaskHandler(this);
          Looper.myQueue().addIdleHandler(this);

          synchronized (this) {
             this.notifyAll();
          }
          Looper.loop();
       }
   }
}

我不明白是什么问题。 你的建议是什么?有什么问题?

谢谢

0 个答案:

没有答案