【问题标题】:Download URL using Retrofit使用 Retrofit 下载 URL
【发布时间】:2017-10-09 13:38:12
【问题描述】:

我正在尝试下载我在使用改造的回复中得到的,但我收到了错误:

Failed to invoke public com.squareup.okhttp.ResponseBody() with no args

我不知道问题出在哪里。

我有一个 recyclerView,每个项目都有一个单独的 URL。

这是我的适配器类,带有带有保存选项的弹出菜单:

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()){

                    case R.id.save:

                        final ApiInterface  downloadService = ApiClient.getClient().create(ApiInterface.class);


                        Call<ResponseBody> call = downloadService.downloadFileWithDynamicUrlSync(url);
                        call.enqueue(new Callback<ResponseBody>() {
                            @Override
                            public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
                                if (response.isSuccessful()) {
                                    Log.d("servercontact", "server contacted and has file");

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

                                            Log.d("downloadsuccess", "file download was a success? " + writtenToDisk);
                                            return null;
                                        }
                                    }.execute();
                                }
                                else {
                                    Log.d("failedserver", "server contact failed");
                                }
                            }

                            @Override
                            public void onFailure(Call<ResponseBody> call, Throwable t) {
                                Log.e("error", t.toString());
                            }
                        });
                        break;
                }
                return true;
            }
        });
        popup.show();



private boolean writeResponseBodyToDisk(ResponseBody body) {
        try {
            // todo change the file location/name according to your needs
            File futureStudioIconFile = new File(Environment.DIRECTORY_DOWNLOADS + File.separator + "Future Studio Icon.png");

            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("filedownload", "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;
        }
    }

这是我的改造接口类:

 @Streaming
    @GET
    Call<ResponseBody> downloadFileWithDynamicUrlSync(@Url String fileUrl);

public class ApiClient {

    public static final String Base_Url = "https://newsapi.org/v1/";
    public static Retrofit retrofit = null;

    static Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    public static Retrofit getClient(){
        if(retrofit==null){
            retrofit= new Retrofit.Builder()
                    .baseUrl(Base_Url)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }

        return retrofit;
    }
}

我想下载 URL 以便用户可以离线查看。

【问题讨论】:

    标签: android retrofit


    【解决方案1】:

    你需要使用 OkHttp 3.x 中的okhttp3.ResponseBody(Retrofit 依赖于它)。该错误消息表明您正在使用 OkHttp 2.x 中的类型。

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 2017-02-24
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      相关资源
      最近更新 更多