При подключении файла с помощью @Part () MultipartBody.Part part выполняется дооснащение сотнями пустых запросов. - PullRequest
0 голосов
/ 16 апреля 2020

У меня есть проблема с модификацией 2 (реализация 'com.squareup.retrofit2: retrofit: 2.3.0'), которую я пока не могу решить.

Я отправляю Multipart Отправьте запрос на мой сервер, и все работает нормально, как и ожидалось, если я не прикрепляю файлы. Однако, как только я прикрепляю файл, на модернизацию уходит несколько минут, пока он не отправит 100 с пустых запросов POST и не вызовет onResponse или onFailure .

Мой интерфейс:


    public interface ApiConfig {
        @Multipart
        @POST("update/")
        Call<ResponseBody> update(
                @Part MultipartBody.Part part,
                @Part("session") RequestBody session,
                @Part("staff") RequestBody staff,
                @Part("action") RequestBody action
        );
    }

Мой клиент:


    public static Retrofit getRetrofitInstance() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();

        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();
        }
        return retrofit;
    }

Моя процедура загрузки


    private void uploadFile(Uri fileUri) {
        // Create instance
        ApiConfig apiInterface = RetrofitClient.getRetrofitInstance().create(ApiConfig.class);

        // Get file
        File file = new File(fileUri.getPath());

        // Dummy variable for testing
        RequestBody var = RequestBody.create(MultipartBody.FORM, "1");

        // File
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));

        // Prepare and make call
        Call<ResponseBody> call1 = apiInterface.update(filePart, var, var, var);
        call1.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Log.v(TAG, response.toString());
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.v(TAG, Objects.requireNonNull(t.getMessage()));
            }
        });
    }

Когда мой сервер вызывается, я смотрю запрос POST следующим образом:

     @csrf_exempt
        def update(request):
            if request.method == 'POST':
                data = dict(request.POST.lists())
                print(data)

            return HttpResponse({})

Вывод без вложенного файла выглядит ожидаемым:
{'session': ['1'], 'staff': ['1'], 'action': ['1']} [16 / Apr / 2020 08:37:43] "POST / update / HTTP / 1.1" 200 0

с использованием:

Call<ResponseBody> call1 = apiInterface.update(null, var, var, var);

Однако, когда я прикрепляю файл, он выглядит примерно так (усекается, когда он длится около 4 секунд):
[16 / Apr / 2020 08:24:08] "POST / update / HTTP /1,1 "200 0

{}
{}
{}
{}
{}
{}
{}{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}

{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}{}
{}
{}
{}{}
{}
{}{}
{}
{}
{}
{}
{}
{}{}
{}
{}
{}
{}
{}
{}
{}{}
{}{}{}
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
{}
{}

{}
{}{}
{}

{}
{}
{}
{}

{}

{}{}{}{}
{}
{}
{}{}

{}{}
{}

{}
{}
{}

{}

{}


{}
{}
{}


{}
{}

{}
{}{}

[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
[16/Apr/2020 08:24:09] "POST /update/ HTTP/1.1" 200 0
...