【问题标题】:How can i send string array through volley?我如何通过凌空发送字符串数组?
【发布时间】:2016-06-11 15:13:16
【问题描述】:

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

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

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

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;
    }
}

【问题讨论】:

标签: android gson android-volley


【解决方案1】:

我想通了!您必须重写 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, 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;
    }
}

现在我在请求中传递了一个字符串 json 作为参数,而不是 HashMap。要创建该字符串,使用 Gson 和我想要的 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;}

}

创建字符串 json:

new Gson().toJson(user, ModelSendFilters.class)

【讨论】:

  • 这里有很多关于 POST 不涉及 Java 模型的 JSON 字符串的帖子
猜你喜欢
  • 1970-01-01
  • 2018-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-16
  • 2010-11-22
  • 1970-01-01
相关资源
最近更新 更多