title: how to execute PUT request in Android Volley?
body: <p>I am currently using GsonRequest to issue rest GET requests. Not clear on what to use for PUT requests where I need to send over a whole JSon object to be updated. The Request object will accept PUT but I'm not sure how place the JSon object that is expected.</p>
  
  <p>Here is my json to be PUT:</p>
  
  <pre><code> {
     prop1 : true,
     prop2 : false,
     prop4 : true
   }
  </code></pre>
  
  <p>Here is how its submitted in apiary.io for example:</p>
  
  <pre><code>  var xhr = new XMLHttpRequest();
    xhr.open('PUT', 'http://my.apiary.io/v1/records/{myid}.json');
  
      xhr.send("{\n    \"isEditable\": false,\n    \"isClosed\": true,\n        \"isAvail\": true\n}");
  </code></pre>
  
  <p>I don't know where to put the JSON.</p>
  
  <p>Thanks</p>
  
  <pre><code>public class GsonRequest&lt;T&gt; extends Request&lt;T&gt; {
  
  private final Gson gson ;
  private final Class&lt;T&gt; clazz;
  private final Map&lt;String, String&gt; headers;
  private final Listener&lt;T&gt; listener;
  
  public GsonRequest(int method, String url, Class&lt;T&gt; clazz, Map&lt;String, String&gt; headers,
          Listener&lt;T&gt; listener, ErrorListener errorListener) {
      super(method, url, errorListener);
  
      GsonBuilder gsonBuilder = new GsonBuilder();
      gsonBuilder.registerTypeAdapter(Timestamp.class, new TimestampDeserializer());
      this.gson = gsonBuilder.create();
      this.clazz = clazz;
      this.headers = headers;
      this.listener = listener;
  }
  
  @Override
  public Map&lt;String, String&gt; getHeaders() throws AuthFailureError {
      return headers != null ? headers : super.getHeaders();
  }
  
  @Override
  protected void deliverResponse(T response) {
      listener.onResponse(response);
  }
  
  @Override
  protected Response&lt;T&gt; parseNetworkResponse(NetworkResponse response) {
      try {
          String json = new String(
                  response.data, HttpHeaderParser.parseCharset(response.headers));
          return Response.success(
                  gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
      } catch (UnsupportedEncodingException e) {
          return Response.error(new ParseError(e));
      } catch (JsonSyntaxException e) {
          return Response.error(new ParseError(e));
      }
     }
  }
  </code></pre>
  
  <p>And here are the base getBody methods inside the request. It seems to handle parameters on the Method.PUT, but what if its a JSON string that needs to be sent in the body?</p>
  
  <pre><code>/**
   * Returns the raw POST or PUT body to be sent.
   *
   * @throws AuthFailureError in the event of auth failure
   */
  public byte[] getBody() throws AuthFailureError {
      Map&lt;String, String&gt; params = getParams();
      if (params != null &amp;&amp; params.size() &gt; 0) {
          return encodeParameters(params, getParamsEncoding());
      }
      return null;
  }
  
  /**
   * Converts &lt;code&gt;params&lt;/code&gt; into an application/x-www-form-urlencoded encoded string.
   */
  private byte[] encodeParameters(Map&lt;String, String&gt; params, String paramsEncoding) {
      StringBuilder encodedParams = new StringBuilder();
      try {
          for (Map.Entry&lt;String, String&gt; entry : params.entrySet()) {
              encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
              encodedParams.append('=');
              encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
              encodedParams.append('&amp;');
          }
          return encodedParams.toString().getBytes(paramsEncoding);
      } catch (UnsupportedEncodingException uee) {
          throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
      }
  }
  </code></pre>
  
  <p>Suggested solution:</p>
  
  <pre><code>     // add a Json body.
      public String jsonBody;  
  
    /**
     * Returns the raw POST or PUT body to be sent.
     *
     * @throws AuthFailureError in the event of auth failure
     */
  
  
  
     public byte[] getBody() throws AuthFailureError {
  
       if ((getMethod() == Method.PUT) &amp;&amp; (jsonBody != null))
       {
          return jsonBody.getBytes();  // Encoding required?????
  
       }
       else
       {
         return super.getBody();
       }
  
   }
  </code></pre>