java – 在Volley请求中将图像作为Multipart与其他参数一起发送

我正在使用volley请求向服务器发送一个带有两个参数的请求,它运行正常.现在需求已经改变,我需要向服务器发送至少一个图像或最多3个图像以及其他两个参数.图像必须作为多部分发送.我有以下代码从库中获取图像并将其文件路径存储在列表中.

List<String> imagePathList = imageFilePaths;
List<MultipartBody.Part> partMap = new ArrayList<>();
for (int i = 0; i < imagePathList.size(); i++) {
    Uri fileUri = Uri.parse(imagePathList.get(i));
    RequestBody requestFile = RequestBody.create(
            MediaType.parse(getMimeTypee(FileUtils.getFile(getContext(), fileUri).getAbsolutePath())),
            FileUtils.getFile(getContext(), fileUri)
    );

   MultipartBody.Part body = MultipartBody.Part.createFormData("court_image[" + i + "]", FileUtils.getFile(getContext(), fileUri).getName(), requestFile);
   partMap.add(body);
}

其中imageFilePaths是ArrayList.服务器将接收像court_image [0],court_image [1]等图像,这取决于我在ArrayList中有多少图像路径.

截击请求在这里

RequestQueue queue = Volley.newRequestQueue(getContext());
StringRequest postRequest = new StringRequest(Request.Method.POST, url1,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(mBaseAppCompatActivity, "Success", Toast.LENGTH_SHORT).show();
           }
        },
        new Response.ErrorListener() {

            @Override
            public void one rrorResponse(VolleyError error) {

            }
        }
) {

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        String token = getToken();
        params.put("Authorization", "Bearer " + token);
        params.put("Content-Type", "multipart/form-data");
        return params;
    }

    @Override
    protected Map<String, String> getParams() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("terms", "true");
        params.put("phone", "phoneNo");
        return params;
    }

};
queue.add(postRequest);

现在的事情就是我对多部分事物的新手,在帮助下我可以从图库中获取图像并将其路径存储在ArrayList中,但我不知道如何在此凌空中传递多部分数据请求.请帮忙.

解决方法:

虽然这是一个老问题,但我认为我应该在这里发布我的答案,因为我遇到了同样的问题,并且可以设法解决这个问题.

为了上传图像以及其他一些参数,我使用了凌空.但是,我找到了原始排球库的包装器,它更易于集成以用于多部件请求.因此,我在build.gradle文件添加了以下库.

dependencies {
    compile 'dev.dworks.libs:volleyplus:+'
}

我从build.gradle中删除了原始的volley库,并使用了上面的库,它可以处理具有类似集成技术的多部分和普通请求.

然后我只需编写以下类来处理POST请求操作.

public class POSTMediasTask {
    public void uploadMedia(final Context context, String filePath) {

        String url = getUrlForPOSTMedia(); // This is a dummy function which returns the POST url for you
        SimpleMultiPartRequest multiPartRequestWithParams = new SimpleMultiPartRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("Response", response);
                        // Todo: Do something on success
                    }
                }, new Response.ErrorListener() {
            @Override
            public void one rrorResponse(VolleyError error) {
                // Todo: Handle your error here
            }
        });

        // Add the file here
        multiPartRequestWithParams.addFile("file", filePath);

        // Add the params here
        multiPartRequestWithParams.addStringParam("terms", "SomeTerms");
        multiPartRequestWithParams.addStringParam("phone", "85050055055");

        RequestQueue queue = Volley.newRequestQueue(context);
        queue.add(multiPartRequestWithParams);
    }
}

现在执行如下任务.

new POSTMediasTask().uploadMedia(context, mediaPath);

您可以使用此库一次上传一个文件.但是,我可以通过启动多个任务来管理上传多个文件.

希望有所帮助!

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...