简体   繁体   中英

Retrofit returns null error message with status 400

I have created an app to use REST api developed by me. Android client is written with retrofit library.

there I have created an interface as ObjectIFaceAsync

public interface ObjectIFaceAsync {
@POST("/")
public void addArea(
        @Body Area area,
        Callback<JsonElement> data
);
}

then I implemented it in a button click

Area pojoArea = new Area();
pojoArea.setArea(area.getText().toString());
pojoArea.setDistrict(district.getText().toString());
pojoArea.setProvince(province.getText().toString());
pojoArea.setAreaType(areaType.getSelectedItem().toString());
RestAdapter restAdapter = new RestAdapter.Builder()
    .setEndpoint(URLs.ENDPOINT + "/restaddarea")
    .build();
ObjectIFaceAsync objectIFaceAsync = restAdapter.create(ObjectIFaceAsync.class);

try {
    objectIFaceAsync.addArea(
        pojoArea, new Callback<JsonElement>() {
            @Override
            public void success(JsonElement jsonElement,retrofit.client.Response response) {
                if (jsonElement.getAsJsonObject()
                    .get("status").getAsString().equals("s")) {
                     //show a message 
                } else {
                    //show another message
                }
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                //show a failure message
                }
            }
            );
    } catch (Exception e) {
    }

I have used the same endpoint to and sent a get request that is not having a JSON @Body. It works fine. Here I always get an error. means it always runs the failure method. I got displayed the

retrofitError.getMessage();

and

retrofitError.getResponse().getStatuse();

They show null and 400 respectively.

Can someone tell what I have to do to get this corrected. or what I have done wrong here.

thanks.

Use

retrofitError.getBody()

to get String or

retrofitError.getBodyAs(MyError.class)

if you want to map the error json to object

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