0

我正在尝试创建我的第一个 Android 应用程序,但遇到了问题。

AsyncHttpClient我有使用 https 协议的网络服务,从我使用库的服务器获取数据,

它一直在使用http,但是一旦我将其更改为https,它就会给我一个错误:

No peer certificate

我在网上做了一些研究,我找到了一些如何修复它的建议,但我很乐意让它发挥作用。

这只是我尝试的一些链接: 链接 1 链接 2

我调用网络服务的代码:

    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 异步?

4

1 回答 1

1

这是我用于SSL网络的代码:

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

请注意,我使用自己的.crt文件。您应该使用服务器证书的数据生成您的证书。

于 2014-02-26T15:39:56.087 回答