在app开发中,我们最常见的就是从app客户端向服务端发一个http请求.对于两种基本的web请求方式get和post来说,get请求方式相对比较简单,在此略过不表.本文重点描述一下通过volley进行几种post提交的方式.
1.客户端以普通的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.客户端以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串方式提交.
如果服务端并不支持这种方式呢?比如常见的spring mvc服务端,就很难支持json的请求方式.
那么我们想实现这个目标,就需要使用下面给出的办法.
3.客户端以普通的post方式进行提交,服务端返回json串
首先在Activity类里,继承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);