Volley的get和post请求方式的使用
来源:互联网 发布:linux nbtscan 编辑:程序博客网 时间:2024/05/16 19:29
Volley的get和post请求 其中get和post还分别可以使用
StringRequest、JsonObjectRequest和 JsonArrayRequest三种方式,下面把整理的StringRequest和JsonObjectRequest两种方式的源码付在下面供大家参考:
1、volley请求之Get请求:
private void volley_Get() { //使用StringRequest// String url = "http://httpbin.org/get?site=code&network=tutsplus";// StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {// @Override// public void onResponse(String s) {// Log.e("onResponse===","onResponse-"+s);// tv_content.setText(s);// }// }, new Response.ErrorListener() {// @Override// public void onErrorResponse(VolleyError volleyError) {// tv_content.setText(volleyError.toString());// }// });// request.setTag("abcGet");// MyApplication.getHttpQueues().add(request); //使用JsonObjectRequest String url = "http://httpbin.org/get?site=code&network=tutsplus"; JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { tv_content.setText("请求结果:\n" + jsonObject.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(MainActivity.this, volleyError.toString(), Toast.LENGTH_LONG).show(); } }); request.setTag("abcGet"); MyApplication.getHttpQueues().add(request); }
2、volley请求之Post请求:
private void volley_Post() { //使用StringRequest// String url = "http://httpbin.org/post?";// StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {// @Override// public void onResponse(String s) {//// tv_content.setText("请求结果:\n"+s);// JSONObject jsonObject = null;// try {// jsonObject = new JSONObject(s);// String m = jsonObject.getString("data");// String n = jsonObject.getJSONObject("form").getString("site");// tv_content.setText(m+n);// } catch (JSONException e) {// e.printStackTrace();// }// }// }, new Response.ErrorListener() {// @Override// public void onErrorResponse(VolleyError volleyError) {// tv_content.setText(volleyError.toString());// }// }){// @Override// protected Map<String, String> getParams() throws AuthFailureError {// Map<String,String> hashMap = new HashMap<>();// hashMap.put("site","code");// hashMap.put("network","tutsplus");// return hashMap;// }// };// request.setTag("abcPost");// MyApplication.getHttpQueues().add(request); //使用JsonObjectRequest String url = "http://httpbin.org/post?"; HashMap<String, String> map = new HashMap<>(); map.put("site", "code"); map.put("network", "tutsplus"); JSONObject object = new JSONObject(map); JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, object, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject jsonObject) { tv_content.setText(jsonObject.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { tv_content.setText(volleyError.toString()); } }); request.setTag("abcPost"); MyApplication.getHttpQueues().add(request); }
新建一个Application
public class MyApplication extends Application { public static RequestQueue queues; @Override public void onCreate() { super.onCreate(); queues = Volley.newRequestQueue(getApplicationContext()); } public static RequestQueue getHttpQueues(){ return queues; }}
同时还要注意在AndroidManifest.xml里添加网络权限
<!-- 网络权限 --><uses-permission android:name="android.permission.INTERNET"/>
在application里加上android:name=".MyApplication"
0 0
- Volley的get和post请求方式的使用
- volley的get,post请求
- volley 的get post 请求
- Android Volley框架的几种post提交请求方式和探究POST、GET提交
- Volley框架--网络请求(GET和POST请求的使用)
- Volley框架实现Http的get和post请求
- Volley中的GET和POST请求工具库的封装
- get和post请求方式的区别
- post和get请求方式的区别
- get和post请求方式的区别
- 请求方式Get和Post的区别
- get方式和post请求的区别?
- Post和Get请求方式的区别
- GET和POST请求方式的区别
- http的post和get请求方式
- HttpURLConnection的GET和POST请求方式
- Android开发中使用Volley框架发送请求的get和post方法
- Android Volley框架及其get,post方式的使用
- Linux进程间通信——信号集函数
- Android 开发 Tip 1 -- android:state_checked & android:state_activated
- Unity3D中各种事件函数的执行顺序
- 第四次作业-奇偶阶乘
- Eclipse中Jar包导出的问题
- Volley的get和post请求方式的使用
- javascript(js)中的 substring和substr方法
- Ubuntu下内存不足的解决办法
- CSS3新特性学习
- tensorflow16《TensorFlow实战Google深度学习框架》笔记-08-01 RNN前向传播 code
- FZU2150(Fire Game)(枚举+BFS)
- gbdt源码阅读
- StackOverflowError的一种原因及解决办法
- C语言--宏将一个数字的奇数位和偶数位交换