Android Volley是否支持SSL?
有谁知道Volley是否支持Android中的SSl? 有没有什么方法可以通过Volley来支持SSL?
你可以参考我的工作示例代码。 希望这可以帮助!
public class MainActivity extends AppCompatActivity { private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.textView); String url = "https://192.168.1.100/testvolley"; HurlStack hurlStack = new HurlStack() { @Override protected HttpURLConnection createConnection(URL url) throws IOException { HttpsURLConnection httpsURLConnection = (HttpsURLConnection) super.createConnection(url); try { httpsURLConnection.setSSLSocketFactory(getSSLSocketFactory()); httpsURLConnection.setHostnameVerifier(getHostnameVerifier()); } catch (Exception e) { e.printStackTrace(); } return httpsURLConnection; } }; final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { mTextView.setText(response.toString(5)); } catch (JSONException e) { mTextView.setText(e.toString()); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { mTextView.setText(error.toString()); } }); final RequestQueue requestQueue = Volley.newRequestQueue(this, hurlStack); requestQueue.add(jsonObjectRequest); } // Let's assume your server app is hosting inside a server machine // which has a server certificate in which "Issued to" is "localhost",for example. // Then, inside verify method you can verify "localhost". // If not, you can temporarily return true private HostnameVerifier getHostnameVerifier() { return new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { //return true; // verify always returns true, which could cause insecure network traffic due to trusting TLS/SSL server certificates for wrong hostnames HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); return hv.verify("localhost", session); } }; } private TrustManager[] getWrappedTrustManagers(TrustManager[] trustManagers) { final X509TrustManager originalTrustManager = (X509TrustManager) trustManagers[0]; return new TrustManager[]{ new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return originalTrustManager.getAcceptedIssuers(); } public void checkClientTrusted(X509Certificate[] certs, String authType) { try { if (certs != null && certs.length > 0){ certs[0].checkValidity(); } else { originalTrustManager.checkClientTrusted(certs, authType); } } catch (CertificateException e) { Log.w("checkClientTrusted", e.toString()); } } public void checkServerTrusted(X509Certificate[] certs, String authType) { try { if (certs != null && certs.length > 0){ certs[0].checkValidity(); } else { originalTrustManager.checkServerTrusted(certs, authType); } } catch (CertificateException e) { Log.w("checkServerTrusted", e.toString()); } } } }; } private SSLSocketFactory getSSLSocketFactory() throws CertificateException, KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException { CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream caInput = getResources().openRawResource(R.raw.my_cert); // this cert file stored in \app\src\main\res\raw folder path Certificate ca = cf.generateCertificate(caInput); caInput.close(); KeyStore keyStore = KeyStore.getInstance("BKS"); keyStore.load(null, null); keyStore.setCertificateEntry("ca", ca); String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm(); TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm); tmf.init(keyStore); TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf.getTrustManagers()); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, wrappedTrustManagers, null); return sslContext.getSocketFactory(); } }
国际海事组织,你也应该阅读更多在谷歌的文档 – 安全与HTTPS和SSL
当然是。
Android Volley是一个库,您可以使用它来轻松高效地pipe理通过http的networking操作。 如果底层使用SSL(即https)是完全不相关的。
换句话说:Volley框架是TCP层不可知的 ,SSL只影响TCP层。
- 当我将Firebase中的JSON转换为Java对象时,为什么会出现“无法popupinput”?
- 编辑文本中的android:ems属性是什么?
- android viewPager实现
- Android Real Time Multiplayer – 房间创作失败
- Genymotion如何使用Chrome开发工具进行debugging
- libpng错误:不是PNG文件在Android Studio中显示错误
- android studio 0.4.2:Gradle项目同步失败的错误
- Android IAB。 错误 – 需要validation。 您需要login到您的Google帐户
- 为什么Android Studio报告“URI未注册”?