Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android问题中从HTTP(2)响应下载PDF文件?_Java_Android_Retrofit2 - Fatal编程技术网

Java Android问题中从HTTP(2)响应下载PDF文件?

Java Android问题中从HTTP(2)响应下载PDF文件?,java,android,retrofit2,Java,Android,Retrofit2,我正在尝试使用安卓改型2发送一个HTTP请求,请求应该得到一个PDF文件作为响应。发送请求后,该PDF文件应在后台下载到用户设备 我尝试使用教程来完成这项工作,但没有成功 这是我的GetFileApi: 这回答了你的问题吗@GET调用不接受请求正文,只允许查询参数和标题我尝试了此操作,但没有任何结果,现在应用程序崩溃了 @GET("index.php/get-file") Call<ResponseBody> getPDF(@Header("authorization") Str

我正在尝试使用安卓改型2发送一个HTTP请求,请求应该得到一个PDF文件作为响应。发送请求后,该PDF文件应在后台下载到用户设备

我尝试使用教程来完成这项工作,但没有成功

这是我的GetFileApi


这回答了你的问题吗
@GET
调用不接受
请求正文
,只允许
查询参数
标题
我尝试了此操作,但没有任何结果,现在应用程序崩溃了
 @GET("index.php/get-file")
 Call<ResponseBody> getPDF(@Header("authorization") String token ,@Body String Id);
 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);
    }

}
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;
        }
    }
java.lang.IllegalArgumentException: Non-body HTTP method cannot contain @Body.