使用Volley实现Https请求, Volley SSL 双向自认证证书请求。
2015-12-29 03:17
1206 查看
代码参照:
https://github.com/yuxiaohui78/volleyHttps
In build.gradle
Replace the certificates client.key.p12 and client.truststore with our own certificates in folder assets
Configure the CertificateConfig.java
Then use the following method to send the request.
References:
http://developer.android.com/training/volley/index.html
https://github.com/yuxiaohui78/android-volley
http://code.tutsplus.com/tutorials/an-introduction-to-volley--cms-23800
http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
https://github.com/yuxiaohui78/volleyHttps
How
to use it.
1.
In AndroidManifest.xml add the VolleyHelperApplication
<application android:name="xiaohui.volley.VolleyHelperApplication" ... </application>
In build.gradle
dependencies { compile 'com.mcxiaoke.volley:library:1.0.19' }
2.
Example:
StringRequest
- HTTP/GET
private void stringRequestGetHttpExample(){ VolleyDataRequester.withHttp( this ) .setUrl( HTTP_HOST + IP) .setMethod( VolleyDataRequester.Method.GET ) .setStringResponseListener( new VolleyDataRequester.StringResponseListener() { @Override public void onResponse(String response) { Toast.makeText( MainActivity.this, "HTTP/POST,StringRequest successfully.", Toast.LENGTH_SHORT ).show(); tvResult.setText( response ); } } ) .requestString(); }
StringRequst
HTTP/POST
private void stringRequestPostHttpExample(){ HashMap<String, String> body = new HashMap <String, String>() ; body.put( "name", "xiaohui" ); body.put( "gender", "male" ); VolleyDataRequester.withHttp( this ) .setUrl( HTTP_HOST + POST) .setBody( body ) .setMethod( VolleyDataRequester.Method.POST ) .setStringResponseListener( new VolleyDataRequester.StringResponseListener() { @Override public void onResponse(String response) { Toast.makeText( MainActivity.this, "HTTP/POST,StringRequest successfully.", Toast.LENGTH_SHORT ).show(); tvResult.setText( response ); } } ) .requestString(); }
JsonRequest
HTTPS/GET
private void jsonRequestGetHttpsExample(){ VolleyDataRequester.withDefaultHttps( this ) .setUrl(HTTPS_HOST + IP) .setJsonResponseListener( new VolleyDataRequester.JsonResponseListener() { @Override public void onResponse(JSONObject response) { try { String s = response.getString( "origin" ); tvResult.setText( s ); Toast.makeText( MainActivity.this, "HTTPS/GET, JsonRequest successfully.", Toast.LENGTH_SHORT ).show(); }catch (Exception e){ e.printStackTrace(); } } } ) .setResponseErrorListener( new VolleyDataRequester.ResponseErrorListener() { @Override public void onErrorResponse(VolleyError error) { tvResult.setText( error.getMessage() ); } } ) .requestJson(); }
JsonRequest
HTTP/POST
private void jsonRequestPostHttpsExample(){ JSONObject json = new JSONObject( ); try{ json.put( "name", "xiaohui" ); json.put( "gender", "male" ); }catch (Exception e){ e.printStackTrace(); } VolleyDataRequester.withDefaultHttps( this ) .setUrl(HTTPS_HOST + POST) .setBody( json ) .setJsonResponseListener( new VolleyDataRequester.JsonResponseListener() { @Override public void onResponse(JSONObject response) { try { String data = response.getString( "data" ); tvResult.setText( data); Toast.makeText( MainActivity.this, "HTTPS/POST, JsonRequest successfully.", Toast.LENGTH_SHORT ).show(); }catch (Exception e){ e.printStackTrace(); } } } ) .setResponseErrorListener( new VolleyDataRequester.ResponseErrorListener() { @Override public void onErrorResponse(VolleyError error) { tvResult.setText( error.getMessage() ); } } ) .requestJson(); }
JsonArrayRequest
HTTP/GET
private void jsonArrayRequestGetHttpsExample(){ VolleyDataRequester.withDefaultHttps( this ) .setUrl(HTTPS_JSONARRAY) .setJsonArrayResponseListener( new VolleyDataRequester.JsonArrayResponseListener() { @Override public void onResponse(JSONArray response) { String output = "Array length=" + response.length(); for (int i = 0; i < response.length(); i++){ try { JSONObject json = (JSONObject) response.get( i ); String name = json.getString( "name" ); output += "\n" + i + " - " + name; }catch (Exception e){ e.printStackTrace(); } } tvResult.setText( output ); } } ) .setResponseErrorListener( new VolleyDataRequester.ResponseErrorListener() { @Override public void onErrorResponse(VolleyError error) { tvResult.setText( error.getMessage() ); } } ) .requestJsonArray(); }
3.
Using Self Certified SSL
Replace the certificates client.key.p12 and client.truststore with our own certificates in folder assetsConfigure the CertificateConfig.java
Then use the following method to send the request.
VolleyDataRequester.withSelfCertifiedHttps( this ) .setUrl( You_url) .setJsonResponseListener( new YouJsonRequestListener ()) .requestJson();
Screeshot:
References:
http://developer.android.com/training/volley/index.html
https://github.com/yuxiaohui78/android-volley
http://code.tutsplus.com/tutorials/an-introduction-to-volley--cms-23800
http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
相关文章推荐
- HTTP协议之chunk编码(分块传输编码
- 验证码识别系统以及人工智能总结 神经网络encog图像识别入门
- 在WordPress中实现发送http请求的相关函数解析
- 关于起网络名字的思考
- 轻量级HTTP服务器Nginx(常用配置实例)
- logrotate日志管家来切割日志(tomcat,nginx,httpd的日志)
- nginx 开发一个简单的 HTTP 模块
- ajax请求与传统http请求区分
- twisted 网络通信的简单例子
- http 简单实现
- iOS http访问post 、get
- HTTPS工作原理
- Mac os charles抓包http/https的简单使用介绍(附3.10.1mac破解版下载链接)
- 手机信息的获取(手机IMEI,手机sim卡的IMSI,网络类型(WiFi,LTE(即4G)等),信号强度(蜂窝网络非WiFi),经纬度)
- 基于spark的网络爬虫实现
- Android开启网络adb调试
- HttpURLConnection用法详解
- iOS经典讲解之网络编程 TCP/UDP HTTP
- Android_HttpURLConnection_Get和Post请求
- 网络编程中的spring-servlet.xml文件