2016-06-11 11 views
0

私はこのようなJSONを送信する必要があります:文字列だけではなく、配列を送信私はどのようにボレーを介して文字列配列を送ることができますか?

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

が、ボレー。これは私がボレーを使用しています、私のリクエストクラスである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; 
    } 
} 
+2

[JSONオブジェクトのためのバレーボールPostメソッド]の可能性のある重複します(http:/その文字列を使用Gsonと私の希望JSONボディのモデルを作成するには/stackoverflow.com/questions/25906689/volley-post-method-for-json-object) –

答えて

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

これで、HashMapではなく、リクエストで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;} 

} 

文字列JSONを作成するには:

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

ここには、Javaモデルに関係しないJSON文字列のPOSTについての記事がたくさんあります –

関連する問題