通过Retrofit2发送文件时遇到问题。但是Web API在POSTMAN命中时工作正常。 响应主体中出现错误:
响应{协议= http / 1.1,代码= 404,消息=未找到,URL = MYURL}
Retrofit2终结点:
@Multipart
@POST("FileStorage/UploadDataFile")
Call<ResponseBody> uploadFile(@Part MultipartBody.Part file,@Part("file") RequestBody name);
更新的代码:
// Uploading File
private void uploadFile() {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) +
File.separator + "dbBackup"+
File.separator +"gps_db_4522_190122114644.sqlite");
//File file = new File(mediaPath);
Retrofit retrofit = RetrofitSingleton.getInstance(getActivity().getBaseContext());
final CommonApiInterface commonApiInterface = retrofit.create(CommonApiInterface.class);
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());
commonApiInterface.uploadFile(fileToUpload,filename).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.isSuccessful()) {
Toast.makeText(getActivity(), "Saved Successfully...", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getActivity(), "Save Fail: " + t, Toast.LENGTH_SHORT).show();
}
});
}
任何帮助将不胜感激。
答案 0 :(得分:0)
最后我发现了我的问题。端点不正确
@Multipart
@POST("FileStorage/UploadDataFile")
Call<ResponseBody> uploadFile(@Part MultipartBody.Part file,@Part("file") RequestBody name);
正确的端点是@POST("FileProcessing/UploadDataFile")