1.客户端以普通的post方式进行提交,服务端返回字符串
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
|
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest =
new
StringRequest(Request.Method.POST,httpurl,
new
Response.Listener
@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
//在这里设置需要post的参数
Map
map.put(
"name1"
,
"value1"
);
map.put(
"name2"
,
"value2"
);
return
params;
}
};
requestQueue.add(stringRequest);
|
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
41
42
43
|
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
Map map.put(
"name1"
,
"value1"
);
map.put(
"name2"
,
"value2"
);
JSONObject jsonObject =
new
JSONObject(params);
JsonRequest
new
JsonObjectRequest(Method.POST,httpurl, jsonObject,
new
Response.Listener
@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
// Map
// map.put("name1", "value1");
// map.put("name2", "value2");
// return params;
// }
@Override
public
Map
HashMap
headers.put(
"Accept"
,
"application/json"
);
headers.put(
"Content-Type"
,
"application/json; charset=UTF-8"
);
return
headers;
}
};
requestQueue.add(jsonRequest);
|
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
|
private
class
NormalPostRequest
extends
Request
private
Map
private
Listener
public
NormalPostRequest(String url, Listener
super
(Request.Method.POST, url, errorListener);
mListener = listener;
mMap = map;
}
//mMap是已经按照前面的方式,设置了参数的实例
@Override
protected
Map
return
mMap;
}
//此处因为response返回值需要json数据,和JsonObjectRequest类一样即可
@Override
protected
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);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
Request
new
NormalPostRequest(httpurl,
new
Response.Listener
@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);
|