2014-02-26 85 views
0

我正嘗試創建我的第一個Android應用程序,並且遇到問題。使用SSL協議的AsyncHttpClient

我有web服務與https協議,獲得由我用AsyncHttpClient庫服務器,

它一直與HTTP數據,但一旦我把它改爲HTTPS它給我一個錯誤:

No peer certificate 

我在網上做了一些研究,發現了一些建議如何解決它,但我是abel,使其工作。

這只是一些我試過的鏈接: link 1 link 2

我的代碼來調用Web服務:

AsyncHttpClient client = new AsyncHttpClient(); 

    client.get(QUERY_URL, 
    new JsonHttpResponseHandler() { 

      @Override 
      public void onSuccess(JSONObject jsonObject) { 

       Log.d("test", jsonObject.toString()); 
      } 

      @Override 
      public void onFailure(int statusCode, Throwable throwable, JSONObject error) { 

       Log.e("test", "Error: " + statusCode + " " + throwable.getMessage()); 
      } 
     }); 

在此先感謝。

也許有一些其他的庫你推薦用於調用https異步?

回答

1

這是我用SSL聯網代碼:我用我自己的.CRT文件

private class MobHawkCheck extends AsyncTask<String, Void, JSONObject> { 

     protected JSONObject doInBackground(String... params) { 
      JSONObject json = null; 
      try { 
       // Load CAs from an InputStream 
       // (could be from a resource or ByteArrayInputStream or ...) 
       CertificateFactory cf = CertificateFactory.getInstance("X.509"); 
       // From https://www.washington.edu/itconnect/security/ca/load-der.crt 
       InputStream cert = mActivity.getResources().openRawResource(R.raw.mycertificate); 
       InputStream caInput = new BufferedInputStream(cert); 
       Certificate ca; 
       try { 
        ca = cf.generateCertificate(caInput); 
        System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN()); 
       } finally { 
        caInput.close(); 
       } 
       // Create a KeyStore containing our trusted CAs 
       String keyStoreType = KeyStore.getDefaultType(); 
       KeyStore keyStore = KeyStore.getInstance(keyStoreType); 
       keyStore.load(null, null); 
       keyStore.setCertificateEntry("ca", ca); 

       // Create a TrustManager that trusts the CAs in our KeyStore 
       String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); 
       TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); 
       tmf.init(keyStore); 

       // Create an SSLContext that uses our TrustManager 
       SSLContext context = SSLContext.getInstance("TLS"); 
       context.init(null, tmf.getTrustManagers(), null); 

       // Tell the URLConnection to use a SocketFactory from our SSLContext 
       URL url = new URL("https://mysecureurl.com"); 
       HttpsURLConnection urlConnection = 
         (HttpsURLConnection)url.openConnection(); 
       urlConnection.setSSLSocketFactory(context.getSocketFactory()); 

       BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
       StringBuilder total = new StringBuilder(); 
       String line; 
       while ((line = r.readLine()) != null) { 
        total.append(line); 
       } 
       json = new JSONObject(total.toString()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } catch (KeyManagementException e) { 
       e.printStackTrace(); 
      } catch (NoSuchAlgorithmException e) { 
       e.printStackTrace(); 
      } catch (KeyStoreException e) { 
       e.printStackTrace(); 
      } catch (CertificateException e) { 
       e.printStackTrace(); 
      } 
      return json; 
     } 

     protected void onPostExecute(JSONObject result) { 
      //TODO parse the result JSONObject 
     } 
    } 

多加留意。你應該用你的服務器證書的數據生成你的。

希望它有幫助。

+0

你可以添加例子你怎麼稱呼這個類?你知道我如何獲得證書文件(.crt),如果我沒有訪問服務器,我只是打電話給服務器? – Greg

+0

@Greg你可以很容易地做到這一點http://superuser.com/questions/97201/how-to-save-a-remote-server-ssl-certificate-locally-as-a-file第二個答案應該是正確的,因爲你無法訪問服務器。我像其他任何AsyncTask新的MobHawkCheck()。execute()一樣調用該函數。 – axierjhtjz

+0

我試過你的解決方案,但我有一個錯誤,這只是其中的一部分:致命異常:AsyncTask#1執行doInBackground()時發生錯誤無法在未調用Looper.prepare()的線程中創建處理程序 – Greg