Volley中json请求POST
来源:互联网 发布:ubuntu安装qt5 编辑:程序博客网 时间:2024/05/01 11:13
原文:http://blog.csdn.net/onlysnail/article/details/47905375
请求的是一个json串,但是这里自定义的Request传入的参数不是一个json串。所以还需要在自定义的类中重写一个方法:
- @Override
- public String getBodyContentType() {
- return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
- }
这段代码从Request.java中Copy。
自定义的Request
- public class MyJsonObjectRequest extends JsonRequest<JSONObject> {
-
- String stringRequest;
-
-
-
-
-
-
-
- public MyJsonObjectRequest(String url, String stringRequest,
- Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
- super(Method.POST, url,stringRequest , listener, errorListener);
- this.stringRequest = stringRequest;
- }
-
- @Override
- public String getBodyContentType() {
- return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
- }
-
- @Override
- protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
- try {
- String jsonString = new String(response.data,
- HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
- 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));
- }
- }
请求方法:- private void jsonObjectRequestPostSuccess1(){
- String url = "http://route.showapi.com/213-3";
- Map<String,String> map = DummyData.getDummyData();
- String params = appendParameter(url,map);
-
- MyJsonObjectRequest jsonObjectRequest = new MyJsonObjectRequest(url,params, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- responseText.setText(response.toString());
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- responseText.setText(error.getMessage());
- }
- });
- mQueue.add(jsonObjectRequest);
- }
-
-
-
- private String appendParameter(String url,Map<String,String> params){
- Uri uri = Uri.parse(url);
- Uri.Builder builder = uri.buildUpon();
- for(Map.Entry<String,String> entry:params.entrySet()){
- builder.appendQueryParameter(entry.getKey(),entry.getValue());
- }
- return builder.build().getQuery();
- }
当然,也并非一定要自定义一个JsonObjectRequest不可,如果了解Request的继承关系和参数的传递流程,直接使用JsonObjectRequest也是可以的。
-
-
-
- private void jsonObjectRequestPostSuccess2(){
- String url = "http://route.showapi.com/213-3";
- Map<String,String> params = DummyData.getDummyData();
- final String mRequestBody = appendParameter(url,params);
-
- JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url,null, new Response.Listener<JSONObject>() {
- @Override
- public void onResponse(JSONObject response) {
- responseText.setText(response.toString());
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- responseText.setText(error.getMessage());
- }
- }){
- @Override
- public String getBodyContentType() {
- return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
- }
-
- @Override
- public byte[] getBody() {
- try {
- return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
- } catch (UnsupportedEncodingException uee) {
- VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
- mRequestBody, PROTOCOL_CHARSET);
- return null;
- }
- }
- };
- mQueue.add(jsonObjectRequest);
- }
-
- private String appendParameter(String url,Map<String,String> params){
- Uri uri = Uri.parse(url);
- Uri.Builder builder = uri.buildUpon();
- for(Map.Entry<String,String> entry:params.entrySet()){
- builder.appendQueryParameter(entry.getKey(),entry.getValue());
- }
- return builder.build().getQuery();
- }
总结:使用JsonObjectRequest 请求服务器,它的参数是以 json 串 的格式传输给服务器的,考虑服务器是否支持这种格式,可以在JsonRequest的PROTOCOL_CONTENT_TYPE中看到它传递参数的类型。如果服务器支持的是 http:...?key1=value1&key2=value2这种格式,也就是对应Request.java中的PROTOCOL_CONTENT_TYPE变量类型。就需要做一些改动,有两种方式来实现。第一种方式是定义一个类继承JsonObjectRequest,第二种直接使用JsonObjectRequest。原理是相同的,重点是重写匿名内部类的getBodyContentType这个方法。不能重写getParams这个方法类传参。
使用StringRequest 如果是带参数,或者Http Header,或者其他,可以重写它匿名内部类方法,如getParams(),getHeader()等等。
0 0