title: How to upload file using Volley library in android?
body: <p>I already have a sub class of <code>Request&lt;JSONObject&gt;</code> that is used for http post to the server. The problem is, I have no idea on how can I add a parameter for a file. Posting string to the server is easy. but I need to add file as a different parameter. How can I do it?</p>
  <pre><code>public class AddNewPetRequest extends Request&lt;JSONObject&gt; {
  
      private Response.Listener&lt;JSONObject&gt; listener;
  
      public AddNewPetRequest(String url, Map&lt;String, String&gt; params,
                                  Response.Listener&lt;JSONObject&gt; reponseListener, Response.ErrorListener errorListener) {
          super(Request.Method.GET, url, errorListener);
          this.listener = reponseListener;
          this.params = params;
      }
  
      public AddNewPetRequest(int method, String url, Map&lt;String, String&gt; params,
                                  Response.Listener&lt;JSONObject&gt; reponseListener, Response.ErrorListener errorListener) {
          super(method, url, errorListener);
          this.listener = reponseListener;
          this.params = params;
      }
  
      protected Map&lt;String, String&gt; getParams()
              throws com.android.volley.AuthFailureError {
          return params;
      };
  
      @Override
      protected Response&lt;JSONObject&gt; parseNetworkResponse(NetworkResponse response) {
          try {
              String jsonString = new String(response.data,
                      HttpHeaderParser.parseCharset(response.headers));
              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));
          }
      }
  
      @Override
      protected void deliverResponse(JSONObject response) {
          // TODO Auto-generated method stub
          listener.onResponse(response);
      }
  }
  </code></pre>
  <p><strong>UPDATE QUESTION:</strong>
  I followed the pattern of one of the answers here in stackoverflow and I came up with this implementation:</p>
  <pre><code>public class MultipartRequest extends Request&lt;String&gt; {
  
  private MultipartEntity entity = new MultipartEntity();
  
  private final Response.Listener&lt;String&gt; mListener;
  private HashMap&lt;String, String&gt; mParams;
  
  
  public MultipartRequest(String url, Response.ErrorListener errorListener, Response.Listener&lt;String&gt; listener)
  {
      super(Method.POST, url, errorListener);
      mListener = listener;
  
      buildMultipartEntity();
  }
  
  private void buildMultipartEntity()
  {
      entity.addPart(&quot;profile_picture&quot;, new FileBody(new File(&quot;/storage/emulated/0/Pictures/VSCOCam/2015-07-31 11.55.14 1.jpg&quot;)));
      try
      {
          entity.addPart(&quot;user_id&quot;, new StringBody(&quot;15&quot;));
          entity.addPart(&quot;name&quot;, new StringBody(&quot;Bogs&quot;));
          entity.addPart(&quot;gender&quot;, new StringBody(&quot;Male&quot;));
          entity.addPart(&quot;date_birth&quot;, new StringBody(&quot;1999-12-5&quot;));
          entity.addPart(&quot;breed&quot;, new StringBody(&quot;monkey&quot;));
      }
      catch (UnsupportedEncodingException e)
      {
          VolleyLog.e(&quot;UnsupportedEncodingException&quot;);
      }
  }
  
  @Override
  public String getBodyContentType()
  {
      return entity.getContentType().getValue();
  }
  
  @Override
  public byte[] getBody() throws AuthFailureError
  {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try
      {
          entity.writeTo(bos);
      }
      catch (IOException e)
      {
          VolleyLog.e(&quot;IOException writing to ByteArrayOutputStream&quot;);
      }
      return bos.toByteArray();
  }
  
  @Override
  protected Response&lt;String&gt; parseNetworkResponse(NetworkResponse response)
  {
      return Response.success(&quot;Uploaded&quot;, getCacheEntry());
  }
  
  @Override
  protected void deliverResponse(String response)
  {
      mListener.onResponse(response);
     
  }
  </code></pre>
  <p>When I add this request to my request queue, it responded with a  <code>com.android.volley.TimeoutError</code> but if a check the data base, the request  executes and add items to the table but the profile picture upload has only 1 byte of size. another problem, my database item added twice.</p>