多部分请求com.android.volley.ServerError

时间:2017-07-03 14:18:42

标签: android android-volley multipart

我必须发送表单

的多部分请求

file:file,data:{" DocTypeID":1," DriverID":2}

我在做的是:

MultipartRequest multipartRequest = new MultipartRequest(ServiceUrls.BASE_URL + ServiceUrls.DOCUMENT_BASE_URL,getHeader(), new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            progress.dismiss();
        }
    }, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            progress.dismiss();
        }
    },file,object){
        @Override
        protected Response<String> parseNetworkResponse(NetworkResponse response) {
            responseCode = response.statusCode;
            handler = new Handler(Looper.getMainLooper());
            runnable = new Runnable() {
                @Override
                public void run() {
                    responseAction();
                }
            };
            handler.postDelayed(runnable, 300);
            return super.parseNetworkResponse(response);
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(multipartRequest);

我的MultipartRequest类的代码如下

public class MultipartRequest extends Request<String> {

private MultipartEntityBuilder builder = MultipartEntityBuilder.create();
private static final String FILE_PART_NAME = "file";
private static final String JSON_PART_NAME = "data";
private final Map<String, String> mHeaders;
private final Response.Listener<String> mListener;
private final File mFilePart;
private final JSONObject mJsonObjectPart;
private HttpEntity mHttpEntity;


public MultipartRequest(String url,Map<String, String> headers, Response.ErrorListener errorListener, Response.Listener<String> listener, File file, JSONObject jsonObject)
{
    super(Method.POST, url, errorListener);
    mHeaders = headers;
    mListener = listener;
    mFilePart = file;
    mJsonObjectPart=jsonObject;
    mHttpEntity = buildMultipartEntity();
    buildMultipartEntity();
}

private HttpEntity buildMultipartEntity()
{
    builder.addPart(FILE_PART_NAME, new FileBody(mFilePart));
    builder.addPart(JSON_PART_NAME, new StringBody(mJsonObjectPart.toString(), ContentType.TEXT_PLAIN));
    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
public Map getHeaders() throws AuthFailureError {
    return (mHeaders!=null)?mHeaders:super.getHeaders();
}

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

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

当我运行代码时,我收到了volley.ServerError,我无法弄清楚我做错了什么。 任何帮助,将不胜感激。 我正确地制作了JSONObject。

0 个答案:

没有答案