【发布时间】:2019-05-14 16:08:44
【问题描述】:
我在我的应用程序上使用 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();
}
});
}
【问题讨论】:
-
我认为你不应该使用 Retrofit 和 AsyncTask。
标签: java android retrofit retrofit2