gpt4 book ai didi

android - 使用 Retrofit 下载 URL

转载 作者:行者123 更新时间:2023-11-29 01:06:57 31 4
gpt4 key购买 nike

我正在尝试下载我在使用 retrofit 的响应中得到的内容,但出现错误:

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;
}
}

这是我改造的接口(interface)类:

 @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 以便用户可以离线查看。

最佳答案

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

关于android - 使用 Retrofit 下载 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46199554/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com