使用多部分改造2.0.2图像上传 [英] Retrofit 2.0.2 image upload using multipart
本文介绍了使用多部分改造2.0.2图像上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Retrofit的新手.我想上传带有不同参数(例如名称,dob,移动设备)的单个图片.我不知道我在哪里错了,请指导我.我遵循此 LINK
I am new for Retrofit I wanna to upload single image with different params like name,dob,mobile. i don't know where i am wrong Please guide me. I follow this LINK
这是我的代码
界面
@Multipart
@POST("signup")
Call<ResponseBody> getSignup(@Part("name") RequestBody name, @Part("email") RequestBody email, @Part("dob") RequestBody dob, @Part("phone") RequestBody phone, @Part("IMEI") RequestBody IMEI, @Part MultipartBody.Part file);
上传代码
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
MultipartBody.Part.createFormData("image", file.getName(), requestFile);
RequestBody name =
RequestBody.create(
MediaType.parse("multipart/form-data"), et_name.getText().toString());
RequestBody email =
RequestBody.create(
MediaType.parse("multipart/form-data"), et_email.getText().toString());
RequestBody dob =
RequestBody.create(
MediaType.parse("multipart/form-data"), et_dob.getText().toString());
RequestBody mobile =
RequestBody.create(
MediaType.parse("multipart/form-data"), et_mobile.getText().toString());
RequestBody imei =
RequestBody.create(
MediaType.parse("multipart/form-data"), IMEI);
Call<ResponseBody> responseBodyCall = apiInterface.getSignup(name, email, dob, mobile, imei, body);
responseBodyCall.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
String response_two = response.body().toString();
Log.i(TAG, "onResponse: " + response_two);
// startActivity(new Intent(this, OTPActivity.class));
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("Upload error:", t.getMessage());
}
});
推荐答案
Retrofit 2.0及更高版本尝试此答案
Retrofit 2.0 onwards try this answer
@Multipart
@POST("/service/users/add_user")
Call<AddUsersModel> addUser(@Part("user_name") RequestBody userName, @Part("user_last_name") RequestBody lastName, @Part("user_password") RequestBody password,
@Part("user_email") RequestBody email, @Part("user_mobile") RequestBody mobile, @Part MultipartBody.Part user_profile);
MulitpartBody.Part 转换的文件路径
MultipartBody.Part profileImageBody = null;
RequestBody reqFile = null;
try {
String filePath = CameraIntentUtil.getFIlePath();
Log.d(TAG, "createAccount: filePath:" + filePath);
if (filePath != null) {
File file = new File(filePath);
reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
// "user_profile" is my post request key
profileImageBody = MultipartBody.Part.createFormData("user_profile", file.getName(), reqFile);
}
} catch (Exception e) {
Log.e(TAG, "createAccount: ", e);
}
要转换为 RequestBody 的字符串
public static RequestBody stringToRequestBody(String data) {
return RequestBody.create(MediaType.parse("multipart/form-data"), data);
}
答案很晚.但是我希望它对任何人都有帮助
Very late answer.. But I hope it help atleast anyone
这篇关于使用多部分改造2.0.2图像上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文