Reputation: 235
I have tried to upload multiple images using volley library
in a single web service
. but, only the last image is getting uploaded. Those previous images are getting replaced with the null. I want to know is it possible with volley library, if not can you plz suggest me other libraries. I'm a newbie to android I only know volley for uploading images.
//JSON Request
public MySampleImageUpload() {
JSONRequestResponse mResponse = new JSONRequestResponse(mContext);
Bundle parms = new Bundle();
parms.putString("key_meail", "[email protected]");
parms.setFile("key_url", image_path);
mResponse.getResponse("sample_upload_data_url", REQUEST_CODE, this,
parms);
}
// In SetFile & getResponse code
package com.fartogram.utils;
import java.io.File;
import org.json.JSONObject;
import android.content.Context;
import android.os.Bundle;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.examples.toolbox.MultipartRequest;
import com.android.volley.examples.toolbox.MyVolley;
import com.android.volley.toolbox.JsonObjectRequest;
public class JSONRequestResponse {
public JSONRequestResponse(Context cntx) {
mContext = cntx;
}
private final Context mContext;
private int reqCode;
private IParseListener listner;
private boolean isFile = false;
private String file_path = "", key = "";
public void getResponse(String url, final int requestCode,
IParseListener mParseListener) {
getResponse(url, requestCode, mParseListener, null);
}
public void getResponse(String url, final int requestCode,
IParseListener mParseListener, Bundle params) {
this.listner = mParseListener;
this.reqCode = requestCode;
Response.Listener<JSONObject> sListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (listner != null) {
listner.SuccessResponse(response, reqCode);
}
}
};
Response.ErrorListener eListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (listner != null) {
listner.ErrorResponse(error, reqCode);
}
}
};
if (!isFile) {
JsonObjectRequest jsObjRequest = new JsonObjectRequest(
Request.Method.GET, url, null, sListener,
eListener);
MyVolley.getRequestQueue().add(jsObjRequest);
} else {
if (file_path != null) {
File mFile = new File(file_path);
MultipartRequest multipartRequest =
new MultipartRequest(url,eListener, sListener, key, mFile, params);
MyVolley.getRequestQueue().add(multipartRequest);
}
}
}
public boolean isFile() {
return isFile;
}
public void setFile(String param, String path) {
if (path != null && param != null) {
key = param;
file_path = path;
this.isFile = true;
}
}
}
Upvotes: 3
Views: 7773
Reputation: 361
public class CustomMultiRequest extends Request<JSONObject> {
private MultipartEntity entity = new MultipartEntity();
private static final String FILE_PART_NAME = "postfieldname[]";
private final Response.Listener<JSONObject> mListener;
private final ArrayList<File> mFilePart;
private final ArrayList<PostEntityModel> mStringPart;
public CustomMultiRequest(String url, Response.ErrorListener errorListener, Response.Listener<JSONObject> listener,ArrayList<File> files, ArrayList<PostEntityModel> stringPart)
{
super(Method.POST, url, errorListener);
mListener = listener;
mFilePart = files;
mStringPart = stringPart;
buildMultipartEntity();
}
private void buildMultipartEntity()
{
for (File file : mFilePart){
entity.addPart(FILE_PART_NAME, new FileBody(file));
}
try
{
for(PostEntityModel postEntityModel : mStringPart){
entity.addPart(postEntityModel.getName(), new StringBody(postEntityModel.getValue()));
}
}
catch (UnsupportedEncodingException e)
{
VolleyLog.e("UnsupportedEncodingException");
}
}
@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("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
@Override
protected Response<JSONObject> 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)
{
mListener.onResponse(response);
}
}
If you use Gradle, add this to your build.gradle file:
compile('org.apache.httpcomponents:httpmime:4.3.6') {
exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
Upvotes: 1