首先簡(jiǎn)單描述一下Google的Android開(kāi)發(fā)團(tuán)隊(duì)在2013年推出的一個(gè)網(wǎng)絡(luò)通信框架Volley.它的設(shè)計(jì)目標(biāo)是進(jìn)行數(shù)據(jù)量不大,但通信頻繁的網(wǎng)絡(luò)操作,而對(duì)于大數(shù)據(jù)量的網(wǎng)絡(luò)操作,比如下載文件等,Volley的表現(xiàn)就不盡如人意。
在app開(kāi)發(fā)中,我們最常見(jiàn)的就是從app客戶(hù)端向服務(wù)端發(fā)一個(gè)http請(qǐng)求.對(duì)于兩種基本的web請(qǐng)求方式get和post來(lái)說(shuō),get請(qǐng)求方式相對(duì)比較簡(jiǎn)單,在此略過(guò)不表.本文重點(diǎn)描述一下通過(guò)volley進(jìn)行幾種post提交的方式.
1.客戶(hù)端以普通的post方式進(jìn)行提交,服務(wù)端返回字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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() { //在這里設(shè)置需要post的參數(shù) Map<String, String> map = new HashMap<String, String>(); map.put( "name1" , "value1" ); map.put( "name2" , "value2" ); return params; } }; requestQueue.add(stringRequest); |
2.客戶(hù)端以json串的post請(qǐng)求方式進(jìn)行提交,服務(wù)端返回json串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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()方法,在此處設(shè)置post需要提交的參數(shù)根本不起作用 //必須象上面那樣,構(gòu)成JSONObject當(dāng)做實(shí)參傳入JsonObjectRequest對(duì)象里 //所以這個(gè)方法在此處是不需要的 // @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); |
看了上面這段代碼,會(huì)覺(jué)得volley這個(gè)框架實(shí)在是還不夠完善,使用JsonObjectRequest對(duì)象提交一個(gè)post請(qǐng)求,如果有參數(shù)需要提交,就必須以JSONObject的json串方式提交.
如果服務(wù)端并不支持這種方式呢?比如常見(jiàn)的spring mvc服務(wù)端,就很難支持json的請(qǐng)求方式.
那么我們想實(shí)現(xiàn)這個(gè)目標(biāo),就需要使用下面給出的辦法.
3.客戶(hù)端以普通的post方式進(jìn)行提交,服務(wù)端返回json串
首先在Activity類(lèi)里,繼承Request實(shí)現(xiàn)一個(gè)NormalPostRequest類(lèi)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 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是已經(jīng)按照前面的方式,設(shè)置了參數(shù)的實(shí)例 @Override protected Map<String, String> getParams() throws AuthFailureError { return mMap; } //此處因?yàn)閞esponse返回值需要json數(shù)據(jù),和JsonObjectRequest類(lèi)一樣即可 @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); } } |
接下來(lái)的調(diào)用方式和前面差不多,生成一個(gè)Request實(shí)例,加入隊(duì)列中即可.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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環(huán)境下測(cè)試通過(guò).
聯(lián)系客服