网络通信框架Volley.它的设计目标是进行数据量不大,但通信频繁的网络操作,而对于大数据量的网络操作,比如下载文件等,Volley的表现就不尽如人意。
在app开发中,我们最常见的就是从app客户端向服务端发一个http请求.对于两种基本的web请求方式get和post来说,get请求方式相对比较简单,在此略过不表.本文重点描述一下通过volley进行几种post提交的方式.
1.客户端以普通的post方式进行提交,服务端返回字符串
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = newStringRequest(Request.Method.POST,httpurl,
newResponse.Listener<string>() {
@Override
publicvoid onResponse(String response) {
Log.d(TAG,"response -> " + response);
}
},newResponse.ErrorListener() {
@Override
publicvoid onErrorResponse(VolleyError error) {
Log.e(TAG, error.getMessage(), error);
}
}) {
@Override
protectedMap<string, string=""> getParams() {
//在这里设置需要post的参数
Map<string, string=""> map = newHashMap<string, string="">();
map.put("name1","value1");
map.put("name2","value2");
returnparams;
}
};
requestQueue.add(stringRequest);
2.客户端以json串的post请求方式进行提交,服务端返回json串
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
Map<string, string=""> map = newHashMap<string, string="">();
map.put("name1","value1");
map.put("name2","value2");
JSONObject jsonObject = newJSONObject(params);
JsonRequest<jsonobject> jsonRequest = newJsonObjectRequest(Method.POST,httpurl, jsonObject,
newResponse.Listener<jsonobject>() {
@Override
publicvoid onResponse(JSONObject response) {
Log.d(TAG,"response -> " + response.toString());
}
},newResponse.ErrorListener() {
@Override
publicvoid 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
publicMap<string, string=""> getHeaders() {
HashMap<string, string=""> headers = newHashMap<string, string="">();
headers.put("Accept","application/json");
headers.put("Content-Type","application/json; charset=UTF-8");
returnheaders;
}
};
requestQueue.add(jsonRequest);
3.客户端以普通的post方式进行提交,服务端返回json串
首先要继承Request实现一个NormalPostRequest类,
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);