我正在尝试使用翻新版发送多部分帖子请求,但无法执行。以下是我需要使用multipart
发布到服务器的发布请求数据。
{
"name":"Akhil",
"email":"[email protected]",
"password":"123456",
"phoneNumber":"9988928289",
"countryCode":"IN",
"pincode":"wddw33",
"latitude":321332.434,
"longitude":232132121.44,
"address":{
"addressLine1":"efefewf",
"addressLine2":"fefewffew"
}
}
我正在这样做。
public interface RegisterInterface {
@Multipart
@POST("register")
Call<Root> signUp(
@Part("name") RequestBody name,
@Part("email") RequestBody email,
@Part("password") RequestBody password,
@Part("phoneNumber") RequestBody phoneNumber,
@Part("countryCode") RequestBody countryCode,
@Part("pincode") RequestBody pincode,
@Part("latitude") RequestBody latitude,
@Part("longitude") RequestBody longitude,
@Part("address") RequestBody address
);
}
private void registerUser(String address){
HashMap<String,String> address1= new HashMap();
address1.put("addressLine1","B-184");
address1.put("addressLine2","New Delhi");
RequestBody uEmail = RequestBody.create(MediaType.parse("email"), email.getValue());
RequestBody uname = RequestBody.create(MediaType.parse("name"), name.getValue());
RequestBody upassword = RequestBody.create(MediaType.parse("password"), password.getValue());
RequestBody phoneNumber = RequestBody.create(MediaType.parse("phoneNumber"), phoneNo.getValue());
RequestBody countryCode = RequestBody.create(MediaType.parse("countryCode"), "IN");
RequestBody pincode = RequestBody.create(MediaType.parse("pincode"), "110092");
RequestBody latitude = RequestBody.create(MediaType.parse("latitude"), 24.0987+"");
RequestBody longitude = RequestBody.create(MediaType.parse("longitude"), 43.09567+"");
RequestBody uaddress = RequestBody.create(MediaType.parse("address"), data1);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiUrl.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RegisterInterface vInterface = retrofit.create(RegisterInterface.class);
RegisterationModel imageDataSet = new RegisterationModel(name.getValue(),phoneNo.getValue(),email.getValue(),password.getValue(),22.0987,43.09876,data1,"IN","110092");
Call<Root> serverCom = vInterface.signUp(uname,uEmail,upassword,phoneNumber,countryCode,pincode,latitude,longitude,uaddress);
serverCom.enqueue(new Callback<Root>() {
@Override
public void onResponse(Call<Root> call, Response<Root> response) {
//Data result = response.body().getData();
Log.d("Result",response.body()+"");
if (response!=null) {
Toast.makeText(context, "File Successfully Uploaded", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<Root> call, Throwable t) {
}
});
}
如果有文件的话..
MultipartBody.Part userImage = null;
File file = new File(model.getReceipt());
RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
userImage = MultipartBody.Part.createFormData(ApiConstants.RECEIPT, file.getName(), requestFile);
在您的Api调用中将userImage
作为参数传递...如果只有数据,那么...
HashMap<String, RequestBody> map = new HashMap<>();
map.put("id", RequestBody.create(MultipartBody.FORM, String.valueOf(invoice_id)));
在您的Api调用中将map
作为参数传递...