0

I'm trying to send an HTTP request using Retrofit 2 for Android, the request should get a PDF file in response. After sending the request, that PDF file should be downloaded to the user's device in the background.

I tried using tutorials to do this but not successfully.

Here is my GetFileApi:

 @GET("index.php/get-file")
 Call<ResponseBody> getPDF(@Header("authorization") String token ,@Body String Id);

Here is my ServiceGenerator class:

 public class ServiceGenerator {

    public static final String API_BASE_URL = "https://url/";

    private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(API_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create());

    public static <S> S createService(Class<S> serviceClass){
        Retrofit retrofit = builder.client(httpClient.build()).build();
        return retrofit.create(serviceClass);
    }

}

Here is my Function:

private void downloadFile(){
        GetFileAPI apiInterface = ServiceGenerator.createService(GetFileAPI.class);

        Call<ResponseBody> call = apiInterface.getPDF(token, id);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()){
                    boolean writtenToDisk = writeResponseBodyToDisk(response.body());

                    Log.d("File downloaded! ", String.valueOf(writtenToDisk));
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    private boolean writeResponseBodyToDisk(ResponseBody body) {
        try {
            // todo change the file location/name according to your needs
            File futureStudioIconFile = new File(getExternalFilesDir(null) + File.separator + "download.pdf");

            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];

                long fileSize = body.contentLength();
                long fileSizeDownloaded = 0;

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {
                        break;
                    }

                    outputStream.write(fileReader, 0, read);

                    fileSizeDownloaded += read;

                    Log.d("File Download: " , fileSizeDownloaded + " of " + fileSize);
                }

                outputStream.flush();

                return true;
            } catch (IOException e) {
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            return false;
        }
    }

I get the this error, although this is not the only reason probably why app is not working properly. It is necessary to send the token and id, the token in @Header and id in the @Body in request.

java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body.
Niklaus
  • 159
  • 1
  • 2
  • 11
  • Does this answer your question? [How to send a HTTP-delete with a body in retrofit?](https://stackoverflow.com/questions/41509195/how-to-send-a-http-delete-with-a-body-in-retrofit) – Santanu Sur Nov 08 '19 at 17:41
  • 1
    `@GET` calls **don't** accept `request body` , only `query params` and `headers` are allowed – Santanu Sur Nov 08 '19 at 17:41
  • i tried this but nothing, now the app just crashed – Niklaus Nov 08 '19 at 18:13

0 Answers0