使用Android中的Volley Library上传多个图像

时间:2014-01-30 04:47:07

标签: android android-volley

我尝试在一个volley library中使用web service上传多个图片。但是,只有最后一张图片上传。之前的图像被替换为null。我想知道是否有可能使用排球库,如果没有,你可以建议我其他库。我是android的新手我只知道上传图像的凌空。

// JSON请求

public MySampleImageUpload() {
    JSONRequestResponse mResponse = new JSONRequestResponse(mContext);

    Bundle parms = new Bundle();
    parms.putString("key_meail", "rojesh@demo.com");
    parms.setFile("key_url", image_path);

    mResponse.getResponse("sample_upload_data_url", REQUEST_CODE, this,
            parms);

}

//在SetFile& getResponse代码

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;
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

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);
    }
}

如果您使用Gradle,请将其添加到build.gradle文件中:

 compile('org.apache.httpcomponents:httpmime:4.3.6') {
    exclude module: 'httpclient'
}
compile 'org.apache.httpcomponents:httpclient-android:4.3.5'