Answer the question
In order to leave comments, you need to log in
Retrofit 2, POST request (multipart/form-data) how to pass parameters?
Hello!
I'm trying to send a POST request to the server to create an order. I use Retrofit 2.
I describe the variables:
private Gson gson = new GsonBuilder().create();
private Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl("mySite.ru")
.build();
private APIv1 intface = retrofit.create(APIv1.class);
Map<String, String> params = new HashMap<String, String>();
params.put("price", String.valueOf(OrderProducts.getBoxSumm()));
params.put("number_person", "1");
params.put("payment_type", "cash");
params.put("delivery", "yes");
params.put("client[name]", this.clientname);
params.put("client[phone]", this.clientphone);
params.put("client[address]", this.clientaddress);
List<OrderProducts> orderProducts = OrderProducts.getOrderProductsNew();
for (int i = 0; i < orderProducts.size(); i++) {
OrderProducts p = orderProducts.get(i);
Product product = Product.getProductById(p.productid);
if (product != null) {
params.put("products["+ String.valueOf(i)+"][product_id]", String.valueOf(product.extid));
params.put("products["+ String.valueOf(i)+"][amount]", String.valueOf(p.amount));
}
}
public interface APIv1 {
@Multipart
@POST("/api/v1/order/create")
Response SendOrder(@Body ЧтоДолжноБытьЗдесь?);
}
Answer the question
In order to leave comments, you need to log in
In general, I did it and it worked:
interface
public interface APIv1 {
@Multipart
@POST("/api/v1/order/create")
Call<ResponseBody> SendOrder(@PartMap Map<String, RequestBody> params);
}
RequestBody rb;
LinkedHashMap<String, RequestBody> mp= new LinkedHashMap<>();
rb = RequestBody.create(MediaType.parse("text/plain"), String.valueOf((int)OrderProducts.getBoxSumm()));
mp.put("price", rb);
rb = RequestBody.create(MediaType.parse("text/plain"), "1");
mp.put("number_person", rb);
rb = RequestBody.create(MediaType.parse("text/plain"), "cash");
mp.put("payment_type", rb);
rb = RequestBody.create(MediaType.parse("text/plain"), "yes");
mp.put("delivery", rb);
rb = RequestBody.create(MediaType.parse("text/plain"), this.clientname);
mp.put("client[name]", rb);
rb = RequestBody.create(MediaType.parse("text/plain"), this.clientphone);
mp.put("client[phone]", rb);
rb = RequestBody.create(MediaType.parse("text/plain"), this.clientaddress);
mp.put("client[address]", rb);
List<OrderProducts> orderProducts = OrderProducts.getOrderProductsNew();
for (int i = 0; i < orderProducts.size(); i++) {
OrderProducts p = orderProducts.get(i);
Product product = Product.getProductById(p.productid);
if (product != null) {
rb = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(product.extid));
mp.put("products["+ String.valueOf(i)+"][product_id]", rb);
rb = RequestBody.create(MediaType.parse("text/plain"), String.valueOf(p.amount));
mp.put("products["+ String.valueOf(i)+"][amount]", rb);
}
}
Call<ResponseBody> call = intface.SendOrder(mp);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
Log.v("Upload", "success");
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("Upload error:", t.getMessage());
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question