我无法使用改造上传图片
Posted
技术标签:
【中文标题】我无法使用改造上传图片【英文标题】:i can't upload image using retrofit 【发布时间】:2018-12-20 11:42:59 【问题描述】:我正在尝试使用 post 方法注册用户,用户已注册但图像未上传且没有错误。
我看到了很多类似的问题,但没有得到解决方案。
从chrome扩展rest api请求和html表单上传成功的图片。
我的代码在下面...
@Multipart
@POST("webservicename")
Call<Login> userUpdate1(@Part("device_id") RequestBody deviceid,
@Part("ip_address") RequestBody ipaddress,
@Part("user_id") RequestBody userid,
@Part("profile_images") MultipartBody.Part profile_images,
@Part("bdate") RequestBody bdate,
@Part("mangal_sani") RequestBody mangal_sani,
@Part("spect") RequestBody spect,
@Part("merried_status") RequestBody merried_status,
@Part("name") RequestBody name,
@Part("height") RequestBody height,
@Part("weight") RequestBody weight,
@Part("qualification") RequestBody qualification,
@Part("occupation") RequestBody occupation,
@Part("income") RequestBody income,
@Part("native") RequestBody nativeplace,
@Part("father") RequestBody father,
@Part("mother") RequestBody mother,
@Part("brothers") RequestBody brothers,
@Part("sisters") RequestBody sisters,
@Part("fincome") RequestBody fincome,
@Part("about_me") RequestBody about_me,
@Part("address") RequestBody address,
@Part("email") RequestBody email,
@Part("mobile") RequestBody mobile,
@Part("phone") RequestBody phone,
@Part("job") RequestBody job,
@Part("age") RequestBody age);
我的图片上传编码如下
private void imageupload()
final ProgressDialog pDialog = new ProgressDialog(getActivity());
pDialog.setIndeterminate(true);
pDialog.setMessage("Loding...");
pDialog.setCancelable(false);
if (!pDialog.isShowing())
pDialog.show();
File file;
if (selectedFilePath.length() > 0)
file = new File(selectedFilePath);
else
file = new File(imagename);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("profile_images", file.getName(), reqFile);
RequestBody email = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edemailid.getText().toString());
RequestBody mobile = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edmobileno.getText().toString());
RequestBody name = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edname.getText().toString());
RequestBody occupation = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edoccupation.getText().toString());
RequestBody qualification = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edqualification.getText().toString());
RequestBody bdate = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.eddob.getText().toString());
RequestBody father = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edfathername.getText().toString());
RequestBody mother = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edmothername.getText().toString());
RequestBody sisters = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edsistername.getText().toString());
RequestBody brothers = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edbrothersname.getText().toString());
RequestBody fincome = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edfamilyincome.getText().toString());
RequestBody job = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edjob.getText().toString());
RequestBody income = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edincome.getText().toString());
RequestBody height = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edheight.getText().toString());
RequestBody weight = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edweight.getText().toString());
RequestBody address = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edaddress.getText().toString());
RequestBody phone = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edphone.getText().toString());
RequestBody mangal_sani = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edmangalsani.getText().toString());
RequestBody age = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edage.getText().toString());
RequestBody spect = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edspector.getText().toString());
RequestBody about_me = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.edaboutMe.getText().toString());
RequestBody merried_status = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.spmerridstatus.getSelectedItem().toString());
RequestBody nativeplace = RequestBody.create(MediaType.parse("text/plain"), updateprofileBinding.ednativeplace.getText().toString());
RequestBody DeviceId = RequestBody.create(MediaType.parse("text/plain"), generateID());
RequestBody IpAddress = RequestBody.create(MediaType.parse("text/plain"), getIpAddress());
RequestBody Userid = RequestBody.create(MediaType.parse("text/plain"), UserId);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface service = retrofit.create(ApiInterface.class);
Call<Login> req = service.userUpdate1(DeviceId, IpAddress, Userid, body, bdate, mangal_sani, spect, merried_status, name, height, weight, qualification, occupation, income, nativeplace, father, mother, brothers, sisters, fincome, about_me, address, email, mobile, phone, job, age);
req.enqueue(new Callback<Login>()
@Override
public void onResponse(Call<Login> call, Response<Login> response)
// Do Something
Login mLoginObject = response.body();
String returnedResponse = mLoginObject.status;
String msg = mLoginObject.msg;
String Userid = mLoginObject.user_id;
if (pDialog.isShowing())
pDialog.dismiss();
if (returnedResponse.trim().equals("1"))
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
if (returnedResponse.trim().equals("2"))
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
if (returnedResponse.trim().equals("3"))
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
if (returnedResponse.trim().equals("4"))
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
if (returnedResponse.trim().equals("0"))
Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
@Override
public void onFailure(Call<Login> call, Throwable t)
t.printStackTrace();
);
【问题讨论】:
请使用 Logcat...除了改造之外,您是否尝试过任何其他库? How to Upload Image file in Retrofit 2的可能重复 检查:***.com/questions/47290322/… 【参考方案1】:在您的 Calling 类中创建 Multipart 主体,如下所示;
MultipartBody.Part body = MultipartBody.Part.createFormData("profile_images",file.getName(), RequestBody.create(MediaType.parse("image/*"),file));
这里 profile_images 是文件的参数名称
在您的界面中使用@Part MultipartBody.Part file
而不是@Part("profile_images") MultipartBody.Part profile_images
,因此您的代码如下:-
@Multipart
@POST("webservicename")
Call<Login> userUpdate1(@Part("device_id") RequestBody deviceid,
@Part("ip_address") RequestBody ipaddress,
@Part("user_id") RequestBody userid,
@Part MultipartBody.Part profile_images ...)
我希望它对你有用。
【讨论】:
我的改造库版本是旧的所以它没有上传我编译新的改造库然后我得到解决方案..感谢您的回复。 @bhoomikapatel 问题不是库版本,根据您问题中的代码,问题出在您的界面中 但是我的旧库版本在@part 之后如果我更改库会给出错误传递值然后它不会在接口类中给出错误并且我没有传递值【参考方案2】:试试这个
在你的方法中
MultipartBody.Part filepart = MultipartBody.Part.createFormData("photo",file.getName(), RequestBody.create(MediaType.parse("image/*"),file));
Call<User> call = client.createAccount(filepart);
在您的 API 界面中
public interface UserClient
@Multipart
@POST("apiupdateprofile.php")
Call<User> createAccount(
@Part MultipartBody.Part filepart
);
【讨论】:
在@part之后的接口类中给出错误传递值complusry 如果我在 MultipartBody.Part 中的 @part 之后没有传递值,则添加缺少的注释参数值会给出错误 在你的图片部分试试我的代码。如果我的回答有用,请点击我的回答上的勾号。【参考方案3】:试试这个:
RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
【讨论】:
【参考方案4】:如果你想上传图片,试试这个方法MediaType.parse("image/*")
MultipartBody.Part body = null;
if (selectedImagePath != null)
File file = new File (selectedImagePath);
RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
body = MultipartBody.Part.createFormData("keyWord", file.getName(), reqFile);
【讨论】:
以上是关于我无法使用改造上传图片的主要内容,如果未能解决你的问题,请参考以下文章