猿问

在 Retrofit 中将表单数据发送到服务器

尝试通过 Retrofit 向服务器发送表单数据,但无法向服务器请求。我想用他们的数据发布一个图像数组。


val builder: MultipartBody.Builder = MultipartBody.Builder().setType(MultipartBody.FORM);

builder.addFormDataPart("device_id",device_UDID)

builder.addFormDataPart("device_token",device_token)

builder.addFormDataPart("device_type","android")

builder.addFormDataPart("country_code",Constant.COUNTRY_CODE)

builder.addFormDataPart("email",signUpBean.email)

builder.addFormDataPart("mobile",signUpBean.phoneNumber)

builder.addFormDataPart("first_name",signUpBean.firstName)

builder.addFormDataPart("last_name",signUpBean.lastName)

builder.addFormDataPart("gender",signUpBean.gender)

builder.addFormDataPart("dob",signUpBean.dob)

builder.addFormDataPart("city",signUpBean.city)

builder.addFormDataPart("bike_type_id","1")

builder.addFormDataPart("bike_model",signUpBean.mfg)

builder.addFormDataPart("bike_manufacturer",signUpBean.mfg)

builder.addFormDataPart("reg_year",signUpBean.mfgYear)

builder.addFormDataPart("liecense_plate",signUpBean.licencePlateNo)

builder.addFormDataPart("bank_ac_name",signUpBean.bnkHolderName)

builder.addFormDataPart("bank_ac_number",signUpBean.bnkAccountNumber)

builder.addFormDataPart("bank_name",signUpBean.bnkName)

builder.addFormDataPart("bank_ifsc_code",signUpBean.ifscCode)

builder.addFormDataPart(

    "profile_pic",

    "profile" + ".jpg",

    RequestBody.create(MediaType.parse("image/*"), file_profile!!)

)

builder.addFormDataPart(

    "provider_documents[0][document]",

    "1" + ".jpg",

    RequestBody.create(MediaType.parse("image/*"), file_profile!!)

)


我尝试了很多解决方案,但无法发布带有数据的图像数组。当我provider_documents从中删除时addFormDataPart工作正常。


// @Multipart

@POST(URLHelper.register)

fun signUp2(@Body builder: RequestBody ): Observable<Registration>

我如何发送Providers_document数组并且它在Postman.

呼啦一阵风
浏览 217回答 2
2回答

富国沪深

虚拟 api 接口。&nbsp;public interface ApiInterface {&nbsp; &nbsp; &nbsp; &nbsp; @Multipart&nbsp; &nbsp; &nbsp; &nbsp; @POST(URLHelper.register)&nbsp; &nbsp; &nbsp; &nbsp; Call<ModelProp> signUp2(@Part List<MultipartBody.Part> photos,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @PartMap Map<String, RequestBody> map;&nbsp; &nbsp; }现在创建要像这样发布的数据。Map<String, RequestBody> partMap = new HashMap<>();List<MultipartBody.Part> images = new ArrayList<>();partMap.put("device_id", createPartFromString(deviceId)); // add data which are common for all images like device_id, device_token, device_type etc.....for (int i=0; i < upFileList.size(); i++){&nbsp; &nbsp;images.add(prepareFilePart("provider_documents["+i+"][document]", imageFile));&nbsp; &nbsp;partMap.add("provider_documents["+i+"][expires_at]", createPartFromString(expiry)); // add image specific data.&nbsp;&nbsp;...&nbsp;..}.....observable = apiInterface.signUp2(images, partMap).createPartFromString方法public RequestBody createPartFromString(String string) {&nbsp; &nbsp; &nbsp; &nbsp; return RequestBody.create(MultipartBody.FORM, string);}prepareFilePart方法private MultipartBody.Part prepareFilePart(String partName, File file){&nbsp; &nbsp; RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);&nbsp; &nbsp; return MultipartBody.Part.createFormData(partName, file.getName(),requestBody);}

慕斯王

像这样使用它:-// @Multipart@POST(URLHelper.register)fun signUp2(@Part builder: MultipartBody ): Observable<Registration>更新 :-&nbsp; private void uploadToServer(String filePath) {&nbsp; &nbsp; &nbsp; &nbsp; showProgressDialog();&nbsp; &nbsp; &nbsp; &nbsp; Retrofit retrofit = RetrofitClient.getRetrofitClient(this);&nbsp; &nbsp; &nbsp; &nbsp; ApiInterface uploadAPIs = retrofit.create(ApiInterface.class);&nbsp; &nbsp; &nbsp; &nbsp; File file = new File(filePath);&nbsp; &nbsp; &nbsp; &nbsp; //compressor.setDestinationDirectoryPath()&nbsp; &nbsp; &nbsp; &nbsp; RequestBody fileReqBody = RequestBody.create(MediaType.parse("image/*"), file);&nbsp; &nbsp; &nbsp; &nbsp; MultipartBody.Part part = MultipartBody.Part.createFormData("fileUpload", file.getName(), fileReqBody);&nbsp; &nbsp; &nbsp; &nbsp; //RequestBody description = RequestBody.create(MediaType.parse("text/plain"), "image-type");&nbsp; &nbsp; &nbsp; &nbsp; RequestBody imgNameReqBody = RequestBody.create(MediaType.parse("multipart/form-data"), "B2B_" + System.nanoTime());&nbsp; &nbsp; &nbsp; &nbsp; uploadAPIs.uploadImage(imgNameReqBody, part).enqueue(new Callback<UploadImageRespose>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(@NonNull Call<UploadImageRespose> call, @NonNull retrofit2.Response<UploadImageRespose> response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.isSuccessful() && response.body() != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (response.body().getCODE().equalsIgnoreCase("SUCCESS")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Activity.this, "Profile Image Upload Succesfully", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hideProgressDialog();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Activity.this, "Some Error&nbsp; occurred, try again", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hideProgressDialog();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(@NonNull Call<UploadImageRespose> call, @NonNull Throwable t) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Timber.d(TAG, t.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hideProgressDialog();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(Activity.this, "Some Error&nbsp; occurred, try again", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }在您的界面中添加以下方法:-&nbsp; &nbsp; @Multipart&nbsp; &nbsp; @POST("Your Path Here")&nbsp; &nbsp; Call<UploadImageRespose> uploadImage(@Part("img_name") RequestBody img_name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Part MultipartBody.Part file);
随时随地看视频慕课网APP

相关分类

Java
我要回答