M
M
Maxim UU2016-09-24 14:25:19
Android
Maxim UU, 2016-09-24 14:25:19

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);

Tell me how to pass parameters to the server correctly?
I am forming the following Map, but I can’t figure out how to correctly describe the Interface and pass the parameter:
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));
            }
        }

Interface
public interface APIv1 {

    @Multipart
    @POST("/api/v1/order/create")
    Response SendOrder(@Body ЧтоДолжноБытьЗдесь?);

}

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim UU, 2016-09-25
@morotkin

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);

}

packing parameters:
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);

            }
        }

query execution:
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());
            }
        });

A
AF, 2016-09-25
@TiPo

Why not

@FormUrlEncoded
@POST("/api/v1/order/create")
Call<Void> sendOrder(@FieldMap Map<String, String> params);

Example with @Multipart : https://futurestud.io/tutorials/retrofit-2-how-to-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question