使用Volley实现Https请求, Volley SSL 双向自认证证书请求。
来源:互联网 发布:c语言cin怎么用 编辑:程序博客网 时间:2024/05/22 04:56
代码参照:
https://github.com/yuxiaohui78/volleyHttps
How to use it.
1. In AndroidManifest.xml add the VolleyHelperApplication
<applicationandroid: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 assets
- Configure 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/
0 0
- 使用Volley实现Https请求, Volley SSL 双向自认证证书请求。
- 自签SSL证书实现Nginx配置https双向认证
- Volley的Https请求
- Volley的Https请求
- Volley支持https请求
- 如何添加SSL证书实现https请求
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- ASIHTTPRequest实现https双向认证请求
- Volley框架支持HTTPS请求。
- Volley框架支持HTTPS请求
- Volley框架支持HTTPS请求。
- Android Volley框架的几种post提交请求方式及上传文件(Https单双向认证)
- volley使用 POST请求
- iOS使用自签名证书实现HTTPS请求
- 黑马程序员——集合的特殊功能之TreeSet集合如何保证元素的唯一及排序的
- iOS开发屏幕旋转锁定横竖屏解决方法
- QT实现文本编辑器软件
- [hdu1890 Robotic Sort]Splay Tree
- ARC兼容 非ARC的类
- 使用Volley实现Https请求, Volley SSL 双向自认证证书请求。
- Asp导出数据库数据到Excel
- 从Python 官方文档找知识点答案的方法
- static全局变量 & 全局变量
- Android ActionBarDrawerToggle、DrawerLayout、ActionBar 结合
- 划分树的建树与查询的理解
- 常换常新 PMC SAS/SATA HBA 不输“乌镇”
- MAC中设置android adb环境变量
- LeetCode Remove Duplicate Letters