Ошибка отправки запроса с использованием модифицированного Android - PullRequest
1 голос
/ 14 мая 2019

Я использую Retrofit в своем приложении для загрузки видеофайла с сервера, в запросе мне нужно сделать запрос Post, в интерфейсе я добавил необходимые параметры .... и в функции JavaЯ также передаю параметры, но когда я пытаюсь запустить код, я получаю сообщение об ошибке:

java.lang.RuntimeException: ошибка при выполнении doInBackground ()

@Headers("Content-Type: application/json; charset=UTF-8")
@Streaming
@POST
Call<ResponseBody> downloadFileStream(@Url String url, @QueryMap Map<String, Object> postdata);

private void downloadFile(String url) { 

    FileDownloadClient fileDownloadClient = retrofit.create(FileDownloadClient.class);

    Call<ResponseBody> call = fileDownloadClient.downloadFileStream(url,postdata);
    postdata.put("user", "test@test.com");
    postdata.put("test", "test");

    Call<ResponseBody> call = fileDownloadClient.downloadFileStream(url, postdata);

    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {

            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... voids) {
                    boolean success = writeResponseBodyToDisk(response.body());

                    return null;
                }
            }.execute();
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(MainActivity.this, "Mal", Toast.LENGTH_LONG).show();
        }
    });
}

1 Ответ

1 голос
/ 14 мая 2019

У меня была такая же проблема, попробуйте это ... это сработало для меня

Ваш интерфейс:

public interface FileDownloadClient {
    @Streaming
    @POST("yourAPI")
    Call<ResponseBody> downloadFileStream(@Body Map<String, Object> postdata);
}

Измените это в вашем файле загрузки:

private void downloadFile() {
        Retrofit.Builder builder = new Retrofit.Builder().baseUrl("yourwebsite/api/")
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create());

        Retrofit retrofit = builder.build();

        FileDownloadClient fileDownloadClient = retrofit.create(FileDownloadClient.class);

        Map<String, Object> postdata = new HashMap<>();
        postdata.put("user", "test@test.com");
        postdata.put("test", "test");

        Call<ResponseBody> call = fileDownloadClient.downloadFileStream(postdata);
}

Grandle:

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.google.code.gson:gson:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'
...