我怎样才能通过凌空发送字符串数组?

时间:2016-06-11 15:13:16

标签: android gson android-volley

我必须发送一个像这样的json:

all()

但是凌空只发送字符串,而不是数组。这是我的请求类,我正在使用volley StringRequest,但我认为还有一种方法可以发送一个数组:

{
    "user_id": "5750891ffe77d2d41732d535",
    "categories" :["5751cd8cb61c39200b368cf3","575b35b9c456c8751cd8530f", "575b35c5c456c8751cd85313"]
}

1 个答案:

答案 0 :(得分:0)

我明白了!你必须覆盖getBody()函数,创建一个自定义的encodeParameters函数,并覆盖getBodyContentType(),将其更改为“application / json”。

public class VolleyRequest extends StringRequest {

    private Map<String, String> params;
    Context context;

    public VolleyRequest(int method, final Context context, String url, Map<String, String> params, final Response.Listener<String> listener) {
        super(method, url, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("WS", volleyError.toString());
            }
        });

       setRetryPolicy(new DefaultRetryPolicy(10000, 10, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));    

       this.params = params;
       this.context = context;
    }

    @Override
    public String getBodyContentType() {
        return "application/x-www-form-urlencoded; charset=UTF-8";
    }

    @Override
    public Map<String, String> getParams() throws AuthFailureError {
        return this.params;
    }
}

看看现在我在我的请求中传递了一个String json作为参数,而不是HashMap。要创建该字符串,请使用Gson和我想要的json body的模型:

public class VolleyRequest extends StringRequest {

    private Map<String, String> params;
    Context context;

    public VolleyRequest(int method, final Context context, String url, String json, final Response.Listener<String> listener) {
        super(method, url, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("WS", volleyError.toString());
            }
        });

       setRetryPolicy(new DefaultRetryPolicy(10000, 10, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));    

       this.params = params;
       this.context = context;
    }

  @Override
    public byte[] getBody() throws AuthFailureError {
        return encodeParameters(getParamsEncoding());
    }

    private byte[] encodeParameters(String paramsEncoding) {
        try {
            if(json!=null) {
                return json.getBytes(paramsEncoding);
            }
            else return  null;
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
        }
    }

    @Override
    public String getBodyContentType() {
        return "application/json";
    }

    @Override
    public Map<String, String> getParams() throws AuthFailureError {
        return this.params;
    }
}

创建String json:

public class ModelSendFilters {
    String user_id;
    String[] categories;

    public String getUser_id() {return user_id;}
    public void setUser_id(String user_id) {this.user_id = user_id;}

    public String[] getCategories() {return categories;}
    public void setCategories(String[] categories) {this.categories = categories;}

}