Android初学者在这里。 我在Multipart POST请求中遇到问题。 我正在使用POSTMAN调用我的API,它返回代码:200 但是当我从我的应用程序调用它时,它返回503。 我发现发生这种情况是因为POSTMAN默认情况下将其作为多部分发送。 我在这里浏览了很多答案,但无法将它们与我的代码关联。
如何将当前请求转换为多部分请求?
这是我的界面:
@Multipart
@POST
Call<JsonObject> Login(@Url String url, @Body JsonObject LoginData);
我的界面如下:
public Call<JsonObject> Logincall(String teller_ID,String password,String ...}
/*somewhere around here i must do MultipartBody.Part...cant figure out where and how */
RetrofitAPI retrofitAPIObj = RETROBUILDER.create(RetrofitAPI.class);
JsonObject LoginData=new JsonObject();
LoginData.addProperty("teller_ID",teller_ID);
LoginData.addProperty("password",password);
LoginData.addProperty("branch",branch);
LoginData.addProperty("terminal",terminal);
LoginData.addProperty("isSecure",isSecure);
return retrofitAPIObj.Login(RetrofitURL.LOGIN, LoginData);
}
预先感谢
答案 0 :(得分:1)
您可以像这样调用Api格式post方法中有类似JsonObject的参数类型
Call<UploadHeadPicResponseModel> uploadHeadPic(@Part MultipartBody.Part file, @Part("json") RequestBody json);
public void doUploadHeadPic(@NonNull String filePath) {
if (!MNetworkUtil.isNetworkAvailable()) {
MToastUtil.show("网络不能连接");
return;
}
File file = new File(filePath);
String json = new Gson().toJson(new UploadHeadPicRequestModel());
if (!file.exists()) {
MToastUtil.show("文件不存在");
return;
}
progressDialog.show();
avatarSimpleDraweeView.setEnabled(false);
MApiManager.getService().uploadHeadPic(
MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file)),
RequestBody.create(MediaType.parse("multipart/form-data"), json))
.enqueue(new OnRetrofitCallbackListener<UploadHeadPicResponseModel>(mActivity) {
@Override
public void onSuccess(UploadHeadPicResponseModel responseModel) {
progressDialog.dismiss();
avatarSimpleDraweeView.setEnabled(true);
if (responseModel != null) {
String serverAvatarUrl = responseModel.data.headPicPath;
if (!TextUtils.isEmpty(serverAvatarUrl)) {
UserModel userModel = MUserManager.getInstance().getUser();
if (userModel != null) {
userModel.setAvatarUrl(serverAvatarUrl);
MUserManager.getInstance().updateOrInsertUserInfo(userModel);
MToastUtil.show("上传头像成功");
}
}
}
}
@Override
public void onFailure(int status, String failureMsg) {
progressDialog.dismiss();
avatarSimpleDraweeView.setEnabled(true);
MToastUtil.show((TextUtils.isEmpty(failureMsg) ? "上传失败" : failureMsg) + " : " + status);
}
});
}
答案 1 :(得分:0)
您可以按照以下步骤创建具有其他属性的多部分实体。
public MultipartBody createMultiPartBody(){
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
builder.addFormDataPart("teller_ID",teller_ID);
builder.addFormDataPart("password",password);
builder.addFormDataPart("branch",branch);
builder.addFormDataPart("terminal",terminal);
builder.addFormDataPart("isSecure",isSecure);
MultipartBody requestBody = builder.build();
return requestBody;
}
现在,通过调用此方法,您将获得multipartBody,可以通过以下代码对其进行解析。
public static void uploadCropImage(String url, RequestBody requestBody, Callback<BasicResponse> callback) {
UploadMultiPartData uploadMultipartData = retrofit.create(UploadMultiPartData.class);
Call<ResponseType> call = uploadCropImageApi.uploadCropImage(url, requestBody);
call.enqueue(callback);
}
这是界面。
public interface UploadMultiPartData {
@POST(UPLOAD_URL)
Call<ResponseType> uploadMultiPartData(
@Url String url,
@Body RequestBody requestBody);
}