【问题标题】:how to perform file upload using Volley in android?如何在 android 中使用 Volley 执行文件上传?
【发布时间】:2015-08-12 08:55:08
【问题描述】:

我想将文件(图像、文档等)从我的 android 应用程序上传到我的服务器。我正在使用 Volley 库在我的应用程序中进行网络调用。

我正在使用以下代码上传文件,但它没有执行任何操作。(最终显示凌空超时错误)

public class MultipartRequest extends Request<String> {

    private MultipartEntity entity = new MultipartEntity();

    private static final String FILE_PART_NAME = "file";


    private final Response.Listener<String> mListener;
    private final File mFilePart;
    private  String mStringPart,accessToken;

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

        mListener = listener;
        mFilePart = file;
        this.accessToken = accessToken;
        buildMultipartEntity();
    }

    private void buildMultipartEntity()
    {
        entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
        try
        {
            entity.addPart("Content-Disposition", new StringBody("form-data"));
            entity.addPart("dir_path", new StringBody("IzEzOjE3"));
        }
        catch (UnsupportedEncodingException e)
        {
            VolleyLog.e("UnsupportedEncodingException");
        }
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = super.getHeaders();

        if (headers == null
                || headers.equals(Collections.emptyMap())) {
            headers = new HashMap<String, String>();
        }

        headers.put("Content-Type", "multipart/form-data");
        headers.put("_pkta",accessToken);


        return headers;
    }
    @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<String> parseNetworkResponse(NetworkResponse response)
    {
        return Response.success("Uploaded", getCacheEntry());
    }

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

请帮助我如何在 android 中实现文件上传(通过 volley 或任何其他方式)

【问题讨论】:

标签: android file file-upload android-volley


【解决方案1】:

试试

public MultiPartRequest(String url, String filePath, Response.Listener<String> listener, Response.ErrorListener errorListener)
{
    super(Method.POST, url, errorListener);
    entity = new  MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    file = new File(filePath);
    mListener = listener;
    buildMultipartEntity();
}

另外,在我的情况下,我没有覆盖 getHeaders,只是通过 addPart 传递了其他值,例如:

    entity.addPart("file", new FileBody(file, "image/jpeg")); // for image
    entity.addPart("id", new StringBody(userid));

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多