我在我的应用程序上使用Retrofit从服务器下载视频文件,在请求中我需要执行发布请求, 在接口上,我添加了所需的参数...。在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();
}
});
}
答案 0 :(得分:1)
我遇到了同样的问题,尝试一下...这对我有用
您的界面:
public interface FileDownloadClient {
@Streaming
@POST("yourAPI")
Call<ResponseBody> downloadFileStream(@Body Map<String, Object> postdata);
}
在您的下载文件上更改此设置:
private void downloadFile() {
Retrofit.Builder builder = new Retrofit.Builder().baseUrl("yourwebsite/api/")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
FileDownloadClient fileDownloadClient = retrofit.create(FileDownloadClient.class);
Map<String, Object> postdata = new HashMap<>();
postdata.put("user", "test@test.com");
postdata.put("test", "test");
Call<ResponseBody> call = fileDownloadClient.downloadFileStream(postdata);
}
爷爷:
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.google.code.gson:gson:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'