如何通过retrofit上传图片?
How to upload image through retrofit?
我浏览了很多 Whosebug 的答案,但无法上传图片。它曾经给我Internal Server Error
。现在我找到了一个 但它向服务器发送了一个空图像。你能告诉我哪里做错了吗
这是我的请求代码:
@Multipart
@POST("lostandfound")
Call<ResponseBody> uploadLostAndFound(@PartMap Map<String, RequestBody> map);
还有我的 CALL:
Map<String, RequestBody> map = new HashMap<>();
map.put("usermail", toRequestBody("mohammad_sed"));
map.put("content", toRequestBody(content));
map.put("islost", toRequestBody(String.valueOf(isLost)));
map.put("wasfound", toRequestBody(foundPlace));
map.put("tofind", toRequestBody(toFindPlace));
File file = new File(destination);
RequestBody reqFile = RequestBody.create(MediaType.parse("image"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);
map.put("img", body);
ForumService client = Utils.getBuilder().create(ForumService.class);
Call<ResponseBody> call = client.uploadLostAndFound(map);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
mProgressDialog.dismiss();
// Toast.makeText(getApplicationContext(), "Request created", Toast.LENGTH_SHORT).show();
} else {
mProgressDialog.dismiss();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
mProgressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Failed to upload!", Toast.LENGTH_SHORT).show();
}
});
// This method converts String to RequestBody
public static RequestBody toRequestBody(String value) {
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
return body;
}
我试过了:
@Multipart
@POST("lostandfound")
Call<ResponseBody> uploadLostAndFound(@Part MultipartBody.Part photo,
@PartMap Map<String, RequestBody> map);
但是这样我得到 Internal server error
。
提前致谢。
我找到了解决方案。
我从 postman
尝试过,它工作正常。
解决方案是我必须使用:
@Multipart
@POST("lostandfound")
Call<ResponseBody> uploadLostAndFound(@Part MultipartBody.Part photo,
@PartMap Map<String, RequestBody> map);
和:
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("img", file.getName(), reqFile);
变化是我必须使用 "img" 而不是 "upload"。这是一个愚蠢的错误 :D
我浏览了很多 Whosebug 的答案,但无法上传图片。它曾经给我Internal Server Error
。现在我找到了一个
这是我的请求代码:
@Multipart
@POST("lostandfound")
Call<ResponseBody> uploadLostAndFound(@PartMap Map<String, RequestBody> map);
还有我的 CALL:
Map<String, RequestBody> map = new HashMap<>();
map.put("usermail", toRequestBody("mohammad_sed"));
map.put("content", toRequestBody(content));
map.put("islost", toRequestBody(String.valueOf(isLost)));
map.put("wasfound", toRequestBody(foundPlace));
map.put("tofind", toRequestBody(toFindPlace));
File file = new File(destination);
RequestBody reqFile = RequestBody.create(MediaType.parse("image"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);
map.put("img", body);
ForumService client = Utils.getBuilder().create(ForumService.class);
Call<ResponseBody> call = client.uploadLostAndFound(map);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
mProgressDialog.dismiss();
// Toast.makeText(getApplicationContext(), "Request created", Toast.LENGTH_SHORT).show();
} else {
mProgressDialog.dismiss();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
mProgressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Failed to upload!", Toast.LENGTH_SHORT).show();
}
});
// This method converts String to RequestBody
public static RequestBody toRequestBody(String value) {
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), value);
return body;
}
我试过了:
@Multipart
@POST("lostandfound")
Call<ResponseBody> uploadLostAndFound(@Part MultipartBody.Part photo,
@PartMap Map<String, RequestBody> map);
但是这样我得到 Internal server error
。
提前致谢。
我找到了解决方案。
我从 postman
尝试过,它工作正常。
解决方案是我必须使用:
@Multipart
@POST("lostandfound")
Call<ResponseBody> uploadLostAndFound(@Part MultipartBody.Part photo,
@PartMap Map<String, RequestBody> map);
和:
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("img", file.getName(), reqFile);
变化是我必须使用 "img" 而不是 "upload"。这是一个愚蠢的错误 :D