简体   繁体   English

改造会返回状态为400的空错误消息

[英]Retrofit returns null error message with status 400

I have created an app to use REST api developed by me. 我已经创建了一个应用程序来使用我开发的REST api。 Android client is written with retrofit library. Android客户端是使用改造库编写的。

there I have created an interface as ObjectIFaceAsync 在那里我创建了一个接口作为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. 我使用了相同的端点并发送了一个没有JSON @Body的get请求。 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. 它们分别显示null400

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 如果您想将错误json映射到对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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