volley几种简单请求,GET+POST String+JSON
2014-06-05 15:31
134 查看
/** * 参见:http://blog.csdn.net/lfdfhl/article/details/12223345 * 最近guolin写了几篇文章详细介绍Volley,所以在此比较系统地学习一下. 在本示例中,主要包括: * 1 利用Volley实现Get,Post请求 * 2 利用Volley实现Json,Map数据请求 * StringRequest * JsonRequest抽象类,无法实例,有两个直接的子类,JsonObjectRequest和JsonArrayRequest * 学习资料: http://blog.csdn.net/guolin_blog/article/details/17482095 */ public class MainActivity extends Activity { private RequestQueue mRequestQueue; private StringRequest mStringRequest; private Context mContext; private JsonObjectRequest mJsonObjectRequest; private TextView text1, text2, text3, text4; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text1 = (TextView) findViewById(R.id.text1); text2 = (TextView) findViewById(R.id.text2); text3 = (TextView) findViewById(R.id.text3); text4 = (TextView) findViewById(R.id.text4); // volley_StringRequest_GET(); volley_StringRequest_POST(); volley_JsonObjectRequest_POST();//JSONObject // volley_JsonObjectRequest_GET(); jsonObjectPostRequest_POST_Map();//Map } /** * 利用StringRequest实现Get请求 */ private void volley_StringRequest_GET() { mContext = this; String url = "http://m.weather.com.cn/data/101010100.html"; // 1 创建RequestQueue对象 mRequestQueue = Volley.newRequestQueue(mContext); // 2 创建StringRequest对象 mStringRequest = new StringRequest(url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.i("Hanjh", "get请求结果:" + response); text1.setText("get请求结果:" + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i("Hanjh", "get请求错误:" + error.toString()); text1.setText("get请求错误:" + error.toString()); } } ); // 3 将StringRequest添加到RequestQueue mRequestQueue.add(mStringRequest); } /** * 利用StringRequest实现Post请求 */ private void volley_StringRequest_POST() { // String url = "http://ave.bolyartech.com/params.php"; String url = "http://demo.wanzi.cc/users/login"; mContext = this; mRequestQueue = Volley.newRequestQueue(mContext); mStringRequest = new StringRequest(Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { text2.setText("volley_post_StringRequest请求结果:" + response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { text2.setText("volley_post_StringRequest请求错误:" + error.toString() + ",errorcode:" + error.networkResponse.statusCode); } } ) { //携带参数,当发出POST请求的时候,Volley会尝试调用StringRequest的父类—— // Request中的getParams()方法来获取POST参数 @Override protected HashMap<String, String> getParams() throws AuthFailureError { HashMap<String, String> hashMap = new HashMap<String, String>(); // hashMap.put("param1", "1"); // hashMap.put("param2", "20"); hashMap.put("user_password", "hanjiahu"); hashMap.put("user_account", "8615161508006"); return hashMap; } }; mRequestQueue.add(mStringRequest); } /** * 利用JsonObjectRequest实现Post请求JSONObject */ private void volley_JsonObjectRequest_POST() { String url = "http://ave.bolyartech.com/params.php"; mContext = this; mRequestQueue = Volley.newRequestQueue(mContext); JSONObject object = new JSONObject(); try { object.put("param1", "44"); object.put("param2", "11"); Log.i("Hanjhpost", "登录上传的信息" + object.toString()); mJsonObjectRequest = new JsonObjectRequest(Method.POST, url, object, createMyReqSuccessListener(), createMyReqErrorListener()); mRequestQueue.add(mJsonObjectRequest); } catch (Exception e) { e.printStackTrace(); } } private Response.Listener<JSONObject> createMyReqSuccessListener() { return new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { Log.i("Hanjh", "volley_post_JsonObjectRequest请求结果:" + response); text3.setText("volley_post_JsonObjectRequest请求结果:" + response.toString()); } catch (Exception e) { text3.setText("Parse error"); } } }; } private Response.ErrorListener createMyReqErrorListener() { return new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { if (null != error) { Log.i("Hanjh", "volley_post_JsonObjectRequest请求错误:" + error.toString() ); text3.setText("volley_post_JsonObjectRequest请求错误:" + error.toString()); } } }; } /** * 利用JsonObjectRequest实现GET数据请求 */ private void volley_JsonObjectRequest_GET() { mContext = this; String url = "http://m.weather.com.cn/data/101010100.html"; // 1 创建RequestQueue对象 mRequestQueue = Volley.newRequestQueue(mContext); JSONObject object = new JSONObject(); try { object.put("user_password", "hanjiahu"); object.put("user_account", "8615161508006"); // 2 创建JsonObjectRequest对象 mJsonObjectRequest = new JsonObjectRequest(Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i("Hanjh_json", "json请求结果:" + response); text4.setText("json请求结果:" + response.toString() ); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i("Hanjh_json", "json请求错误:" + error.toString()); text4.setText("json请求错误:" + error.toString()); } } ); // 3 将JsonObjectRequest添加到RequestQueue mRequestQueue.add(mJsonObjectRequest); } catch (JSONException e) { e.printStackTrace(); } } /** * 使用post上传map格式的信息 */ private void jsonObjectPostRequest_POST_Map() { String url = "http://demo.wanzi.cc/users/login"; HashMap<String, String> mMap = new HashMap<String, String>(); mMap.put("user_password", "hanjiahu"); mMap.put("user_account", "8615161508006"); RequestQueue requestQueue = Volley.newRequestQueue(this); JsonObjectPostRequest jsonObjectPostRequest = new JsonObjectPostRequest(url, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { text4.setText(response.toString()); Log.d("Hanjh", response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(MainActivity.this, "获取失败", Toast.LENGTH_SHORT).show(); Log.d("Hanjh", error.toString()); } }, mMap); requestQueue.add(jsonObjectPostRequest); } }
相关文章推荐
- Android Volley框架的几种post提交请求方式
- Android Volley框架的几种post提交请求方式
- Android Volley框架的几种post提交请求方式
- iOS开发中大部分App的网络数据交换是基于HTTP协议的。本文将简单介绍在Swift中使用HTTP进行网络请求的几种方法。
- Volley请求网络简单学习
- Jersey使用总结一(jersey初步使用和简单post请求几种参数传递,String方式,不含对象操作)
- Android Volley做网络请求的几种用法
- Android网络请求框架:Volley简单使用
- 【Android】Volley做网络请求的几种用法
- Android Volley框架的几种post提交请求方式
- Android Volley框架的几种post提交请求方式
- Volley简介(1) : 简单的网络请求
- Android Volley框架的几种post提交请求方式和探究POST、GET提交
- Volley请求的简单封装,以及applicaation/json数据请求
- Android Volley框架的几种post提交请求方式(xml->xml json->json xml->json)
- Android Volley框架的几种post提交请求方式[转]
- Android Volley框架的几种post提交请求方式
- Android Volley框架的几种post提交请求方式
- Android Volley框架的几种post提交请求方式及上传文件(Https单双向认证)
- Android Https请求的简单使用(Volley Https请求的示例)