简体   繁体   中英

Retrofit POST request using @Body returns empty response body

I am trying to send data to server with end point like this:
https://api.test.com/sales/taken?key=1a2b3c&sales_number=abc&taken[0][id_product]=123&taken[0][qty]=123&taken[1][id_product]=123&taken[1][qty]=123

According to Android Retrofit POST ArrayList , best way to send a list is by using @Body instead of @Field . So i made a model to suit the end point like this:

public class Model {

    @SerializedName("key")
    private String key;

    @SerializedName("sales_number")
    private String sales_number;

    @SerializedName("taken")
    private List<Taken> taken;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getSales_number() {
        return sales_number;
    }

    public void setSales_number(String sales_number) {
        this.sales_number = sales_number;
    }

    public List<NotaAmbilDetailModel> getTaken() {
        return taken;
    }

    public void setTaken(List<NotaAmbilDetailModel> taken) {
        this.taken = taken;
    }
}

and

public class Taken {

    @SerializedName("id_product")
    private int id_product;

    @SerializedName("qty")
    private int qty;

    public int getId_product() {
        return id_product;
    }

    public void setId_product(int id_product) {
        this.id_product = id_product;
    }

    public int getQty() {
        return qty;
    }

    public void setQty(int qty) {
        this.qty = qty;
    }
}

My post interface looks like this:

@POST("/sales/taken")
Call<ResponseModel> sendData(@Body Model model);

Response status is:
Response{protocol=h2, code=200, message=, url=https://api.test.com/sales/taken} .

As you can see, response code is 200 but when i tried to get the response body, java.lang.NullPointerException occurred.

My error log is:

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String ResponseModel.getCode()' on a null object reference
D/Response: LOG: Response{protocol=h2, code=200, message=, url=https://api.test.com/sales/taken}
Response Body: ResponseModel@bb5e4cf

Your api request expecting query params instead of request body.

Try like this

Request

@POST("/sales/taken")
Call<ResponseModel> sendData(@QueryMap Map<String, String> queryMap);

Params for api

Map<String, String> queryMap = new HashMap<>();
queryMap.put("key", "value_for_key");
queryMap.put("sales_number", "value_for_sales_number");
queryMap.put("taken[0][id_product]", "value_for_taken[0][id_product]");
queryMap.put("taken[0][qty]", "value_for_taken[0][qty]");
queryMap.put("taken[1][id_product]", "value_for_taken[1][id_product]");
queryMap.put("taken[1][qty]", "value_for_taken[1][qty]");
....

Your backend has to be accepted post requests as JSON. Your ResponseModel causes this error. I think it's not a server error. You can try to declare code as Integer

Your response is like

LOG: Response{protocol=h2, code=200, message=, url=https://api.test.com/sales/taken}

and you are getting this exception

'java.lang.String ResponseModel.getCode()' on a null object reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM