标签:err get请求 object set except val headers 设置 大数据
1.client以普通的post方式进行提交,服务端返回字符串
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); StringRequest stringRequest = new StringRequest(Request.Method.POST,httpurl, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d(TAG, "response -> " + response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(TAG, error.getMessage(), error); } }) { @Override protected Map<String, String> getParams() { //在这里设置须要post的參数 Map<String, String> map = new HashMap<String, String>(); map.put("name1", "value1"); map.put("name2", "value2"); return params; } }; requestQueue.add(stringRequest);2.client以json串的post请求方式进行提交,服务端返回json串
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); Map<String, String> map = new HashMap<String, String>(); map.put("name1", "value1"); map.put("name2", "value2"); JSONObject jsonObject = new JSONObject(params); JsonRequest<JSONObject> jsonRequest = new JsonObjectRequest(Method.POST,httpurl, jsonObject, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d(TAG, "response -> " + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(TAG, error.getMessage(), error); } }) { //注意此处override的getParams()方法,在此处设置post须要提交的參数根本不起作用 //必须象上面那样,构成JSONObject当做实參传入JsonObjectRequest对象里 //所以这种方法在此处是不须要的 // @Override // protected Map<String, String> getParams() { // Map<String, String> map = new HashMap<String, String>(); // map.put("name1", "value1"); // map.put("name2", "value2"); // return params; // } @Override public Map<String, String> getHeaders() { HashMap<String, String> headers = new HashMap<String, String>(); headers.put("Accept", "application/json"); headers.put("Content-Type", "application/json; charset=UTF-8"); return headers; } }; requestQueue.add(jsonRequest);看了上面这段代码,会认为volley这个框架实在是还不够完好,使用JsonObjectRequest对象提交一个post请求,假设有參数须要提交,就必须以JSONObject的json串方式提交.
private class NormalPostRequest extends Request<JSONObject> { private Map<String, String> mMap; private Listener<JSONObject> mListener; public NormalPostRequest(String url, Listener<JSONObject> listener,ErrorListener errorListener, Map<String, String> map) { super(Request.Method.POST, url, errorListener); mListener = listener; mMap = map; } //mMap是已经依照前面的方式,设置了參数的实例 @Override protected Map<String, String> getParams() throws AuthFailureError { return mMap; } //此处由于response返回值须要json数据,和JsonObjectRequest类一样就可以 @Override protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) { try { String jsonString = new String(response.data,HttpHeaderParser.parseCharset(response.headers)); return Response.success(new JSONObject(jsonString),HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JSONException je) { return Response.error(new ParseError(je)); } } @Override protected void deliverResponse(JSONObject response) { mListener.onResponse(response); } }接下来的调用方式和前面几乎相同,生成一个Request实例,增加队列中就可以.
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); Request<JSONObject> request = new NormalPostRequest(httpurl, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d(TAG, "response -> " + response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(TAG, error.getMessage(), error); } }, params); requestQueue.add(request);以上代码在android 4.3环境下測试通过.
标签:err get请求 object set except val headers 设置 大数据
原文地址:http://www.cnblogs.com/wzjhoutai/p/6835241.html