标签:
在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);
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);
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);
}
}
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);
标签:
原文地址:http://www.cnblogs.com/cy21528/p/4837720.html