Skip to content

Instantly share code, notes, and snippets.

@maheshwarLigade
Forked from ishitcno1/MultipartRequest.java
Created February 11, 2016 20:05
Show Gist options
  • Save maheshwarLigade/cb2d9991dbfeedc819f8 to your computer and use it in GitHub Desktop.
Save maheshwarLigade/cb2d9991dbfeedc819f8 to your computer and use it in GitHub Desktop.

Revisions

  1. @ishitcno1 ishitcno1 revised this gist May 7, 2014. 1 changed file with 17 additions and 10 deletions.
    27 changes: 17 additions & 10 deletions MultipartRequest.java
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    package com.meishixing.tripschedule.util;

    import com.android.volley.AuthFailureError;
    import com.android.volley.NetworkResponse;
    import com.android.volley.Request;
    @@ -24,7 +22,6 @@ public class MultipartRequest extends Request<String> {

    private HttpEntity mHttpEntity;

    private String mFilePath;
    private String mRouteId;
    private Response.Listener mListener;

    @@ -33,27 +30,38 @@ public MultipartRequest(String url, String filePath, String routeId,
    Response.ErrorListener errorListener) {
    super(Method.POST, url, errorListener);

    mFilePath = filePath;
    mRouteId = routeId;
    mListener = listener;
    mHttpEntity = buildMultipartEntity();
    mHttpEntity = buildMultipartEntity(filePath);
    }

    public MultipartRequest(String url, File file, String routeId,
    Response.Listener<String> listener,
    Response.ErrorListener errorListener) {
    super(Method.POST, url, errorListener);

    mRouteId = routeId;
    mListener = listener;
    mHttpEntity = buildMultipartEntity(file);
    }

    private HttpEntity buildMultipartEntity(String filePath) {
    File file = new File(filePath);
    return buildMultipartEntity(file);
    }

    private HttpEntity buildMultipartEntity() {
    private HttpEntity buildMultipartEntity(File file) {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    File file = new File(mFilePath);
    String fileName = file.getName();
    FileBody fileBody = new FileBody(file);
    builder.addPart(KEY_PICTURE, fileBody);
    builder.addTextBody(KEY_PICTURE_NAME, fileName);
    builder.addTextBody(KEY_ROUTE_ID, mRouteId);
    MBLog.d("fileName: " + fileName);
    return builder.build();
    }

    @Override
    public String getBodyContentType() {
    MBLog.d("content type: " + mHttpEntity.getContentType().getValue());
    return mHttpEntity.getContentType().getValue();
    }

    @@ -62,7 +70,6 @@ public byte[] getBody() throws AuthFailureError {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
    mHttpEntity.writeTo(bos);
    MBLog.d("bos: " + bos.toString());
    } catch (IOException e) {
    VolleyLog.e("IOException writing to ByteArrayOutputStream");
    }
  2. @ishitcno1 ishitcno1 created this gist Apr 29, 2014.
    81 changes: 81 additions & 0 deletions MultipartRequest.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    package com.meishixing.tripschedule.util;

    import com.android.volley.AuthFailureError;
    import com.android.volley.NetworkResponse;
    import com.android.volley.Request;
    import com.android.volley.Response;
    import com.android.volley.VolleyLog;

    import org.apache.http.HttpEntity;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.entity.mime.content.FileBody;

    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;

    /**
    * Created by albert on 14-3-21.
    */
    public class MultipartRequest extends Request<String> {
    public static final String KEY_PICTURE = "mypicture";
    public static final String KEY_PICTURE_NAME = "filename";
    public static final String KEY_ROUTE_ID = "route_id";

    private HttpEntity mHttpEntity;

    private String mFilePath;
    private String mRouteId;
    private Response.Listener mListener;

    public MultipartRequest(String url, String filePath, String routeId,
    Response.Listener<String> listener,
    Response.ErrorListener errorListener) {
    super(Method.POST, url, errorListener);

    mFilePath = filePath;
    mRouteId = routeId;
    mListener = listener;
    mHttpEntity = buildMultipartEntity();
    }

    private HttpEntity buildMultipartEntity() {
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    File file = new File(mFilePath);
    String fileName = file.getName();
    FileBody fileBody = new FileBody(file);
    builder.addPart(KEY_PICTURE, fileBody);
    builder.addTextBody(KEY_PICTURE_NAME, fileName);
    builder.addTextBody(KEY_ROUTE_ID, mRouteId);
    MBLog.d("fileName: " + fileName);
    return builder.build();
    }

    @Override
    public String getBodyContentType() {
    MBLog.d("content type: " + mHttpEntity.getContentType().getValue());
    return mHttpEntity.getContentType().getValue();
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
    mHttpEntity.writeTo(bos);
    MBLog.d("bos: " + bos.toString());
    } catch (IOException e) {
    VolleyLog.e("IOException writing to ByteArrayOutputStream");
    }
    return bos.toByteArray();
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
    return Response.success("Uploaded", getCacheEntry());
    }

    @Override
    protected void deliverResponse(String response) {
    mListener.onResponse(response);
    }
    }