如何在 android studio 中通过 okhttps 仅上传最后一个文件上传多个文件

how to upload multiple files in android studio by okhttps only last file uploaded

我正在使用 okhttps 3 将多个文件上传到服务器 但它总是上传最后一个文件而不是所有文件 我的代码在

下面

解释码:-

1.get 用户 ID 形式共享偏好

2.create okhttps 客户端

3 检查文件是否为空

4.create multipartbody 添加参数和数据

5.create 一个 for 循环附加参数

中的文件

6.call 请求正文并获取响应

public static void TaskAdd(final Activity activity, final String task_title, final String employees, final String priority, final String add_due_date, final String task_detail, final File[] file, final FragmentManager fragmentManager, final Dialog dialog) {
        dialog.show();
        Log.d(TAG, "sendImage: Enter");
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
        final String UserId = settings.getString("UserId", "");

        OkHttpClient client = new OkHttpClient();
        Request request;

        // MultipartBody requestBody;
        if (file != null) {
            MultipartBody.Builder body = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("task_title", task_title)
                    .addFormDataPart("employees", employees)
                    .addFormDataPart("priority", priority)
                    .addFormDataPart("add_due_date", add_due_date)
                    .addFormDataPart("task_detail", task_detail)
                    .addFormDataPart("user_id", UserId);
           // body.addFormDataPart("files", file[i].getName(), RequestBody.create(MediaType.parse("Image/*"), file[i]));
//            MultipartBody.Builder body2[]=new MultipartBody.Builder[file.length];
//
//            for  (int i=0;i<file.length;i++) {
//                 body2[i]= body.addFormDataPart("files", file[i].getName(), RequestBody.create(MediaType.parse("Image/*"), file[i]));
//            }

            for (int i = 0; i < file.length; i++) {
                Log.d(TAG, "TaskAdd: File " + file[i]);
                if (file[i].exists()) {
                    Log.d(TAG, "TaskAdd: "+i);
                    //body.addPart()
                    body.addFormDataPart("files", file[i].getName(), RequestBody.create(MediaType.parse("Image/*"), file[i]));
                    body.addFormDataPart("files", file[i].getName(), RequestBody.create(MediaType.parse("Image/*"), file[i]));

                    Log.d(TAG, "TaskAdd: "+ body.getClass().getName());
                } else {
                    Log.d(TAG, "TaskAdd: No file found");
                }
            }

            MultipartBody  requestBody = body.build();
            request = new Request.Builder().url("http://www.****.com/web/aagam/public/add_update_tasks").post(requestBody).build();
        } else {
            RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                    .addFormDataPart("task_title", task_title)
                    .addFormDataPart("employees", employees)
                    .addFormDataPart("priority", priority)
                    .addFormDataPart("add_due_date", add_due_date)
                    .addFormDataPart("task_detail", task_detail)
                    .addFormDataPart("user_id", UserId)
                    .build();
            request = new Request.Builder().url("http://www.****.com/web/aagam/public/add_update_tasks").post(body).build();
        }


        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                dialog.dismiss();
                Log.d(TAG, "OK HTTP 3.0 onFailure: ");

                activity.runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(activity, "Server Speed Slow please Try After SomeTime", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(okhttp3.Call call, final okhttp3.Response response) throws IOException {
                dialog.dismiss();
                final String responseBody = response.body().string().toString();
                Log.d(TAG, "response    " + responseBody);

                activity.runOnUiThread(new Runnable() {
                    public void run() {

                        try {

                            JSONObject jsonObject = new JSONObject(responseBody);
                            String success = jsonObject.getString("success");
                            String message = jsonObject.getString("message");

                            if (success.equals("1")) {
                                Toast.makeText(activity, "Task added successfully", Toast.LENGTH_SHORT).show();
                                fragmentManager.popBackStack();
                            } else {
                                if (message.equalsIgnoreCase("Task added successfully")) {
                                    Toast.makeText(activity, "Successful add task", Toast.LENGTH_SHORT).show();
                                    fragmentManager.popBackStack();
                                } else {
                                    Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
                                }
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });
    }

您正在通过传递相同的名称覆盖相同的文件,将其也更改为 file[] 或传递为 file1 和 file2

 for (int i = 0; i < file.length; i++) {
                    Log.d(TAG, "TaskAdd: File " + file[i]);
                    if (file[i].exists()) {
                        Log.d(TAG, "TaskAdd: "+i);
                        //body.addPart()
                        body.addFormDataPart("files["+i+"]", file[i].getName(), RequestBody.create(MediaType.parse("Image/*"), file[i]));

                        Log.d(TAG, "TaskAdd: "+ body.getClass().getName());
                    } else {
                        Log.d(TAG, "TaskAdd: No file found");
                    }
                }