Last active
April 7, 2019 18:05
-
-
Save ishitcno1/11394069 to your computer and use it in GitHub Desktop.
volley multipart request, post file image, android.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 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); | |
mRouteId = routeId; | |
mListener = listener; | |
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(File file) { | |
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); | |
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); | |
return builder.build(); | |
} | |
@Override | |
public String getBodyContentType() { | |
return mHttpEntity.getContentType().getValue(); | |
} | |
@Override | |
public byte[] getBody() throws AuthFailureError { | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
try { | |
mHttpEntity.writeTo(bos); | |
} 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); | |
} | |
} |
Thanks, it looks like what I need,
but when I'm trying to use MultipartEntityBuilder, every dependency I used for it Gradle causes run time problems. It conflicted with some internal Android libraries.
Can you please share the dependency you used?
Can you give the php code to be used with this
Using HttpEntity is deprecated.
HttpEntity problem solved by this solution.
http://stackoverflow.com/questions/30556605/org-apache-http-httpentity-is-deprecated-how-to-solve-this-error-in-android-s
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, could you please provide a sample for uploading a image using your class above. thank you