无法通过Retrofit获得json响应

时间:2022-10-08 09:26:17

I'm using retrofit 1.9.0 and I had tried the following code to get a response in json format:

我正在使用改装1.9.0,我尝试了以下代码来获得json格式的响应:

public void Execute(String email, String password, Callback<User> callback) {
    final Callback<User> cb = callback;
    RestAdapter restAdapter = buildRestAdapter();
    System.out.println("Email " + email + " passowrd " + password);
    User user = new User();
    user.setEmail(email);
    user.setPassword(password);
    restAdapter.create(YutonAPI.class).postLogin(
            user,
            new Callback<User>() {
                @Override
                public void success(User user, Response response) {
                    System.out.println("succes");
                    System.out.println(response.getBody());
                }

                @Override
                public void failure(RetrofitError error) {
                    System.out.println("error "+ error);
                }
            });
}

So this line of code:

所以这行代码:

System.out.println(response.getBody());

Should give me a response in json format however it didn't work because I'm getting the following output:

应该给我一个json格式的响应,但它不起作用,因为我得到以下输出:

Link: http://i.imgur.com/mBQs1LL.png

链接:http://i.imgur.com/mBQs1LL.png

So this is how my response in json format should look like:

所以这就是我在json格式中的响应应该是这样的:

{
    "user": {
        "image": "https://www.gravatar.com/avatar/e0a190604dc3dd2ee7b66bb95c20ef7f?d=identicon&s=512"
        "email": "a@hotmail.com"
        "name": "a"
        "id": "566dfac21043a31820bf1ae6"
    } -
}

I had already tested it on my server where I was making a post request. Below you can see a screenshot of it:

我已经在我的服务器上测试了它,我正在发布帖子请求。您可以在下面看到它的截图:

Link: http://i.imgur.com/PtEMR12.png

链接:http://i.imgur.com/PtEMR12.png

1 个解决方案

#1


2  

The issue here is that response.getBody() returns a TypedInputStream object, which you can't directly output because it isn't a String.

这里的问题是response.getBody()返回一个TypedInputStream对象,由于它不是String,因此无法直接输出。

To read a TypedInputStream there are several options, as posted in: Retrofit callback get response body, the easiest being:

要读取TypedInputStream,有几个选项,如发布于:Retrofit callback get response body,最简单的方法是:

String body = new String(((TypedByteArray) response.getBody()).getBytes());

If the following error is thrown:

如果抛出以下错误:

java.lang.ClassCastException: retrofit.client.UrlConnectionClient$TypedInputStream cannot be cast to retrofit.mime.TypedByteArray

Then make sure that you set .setLogLevel(RestAdapter.LogLevel.FULL) on the RestAdapter that you use to create the service.

然后确保在用于创建服务的RestAdapter上设置.setLogLevel(RestAdapter.LogLevel.FULL)。

#1


2  

The issue here is that response.getBody() returns a TypedInputStream object, which you can't directly output because it isn't a String.

这里的问题是response.getBody()返回一个TypedInputStream对象,由于它不是String,因此无法直接输出。

To read a TypedInputStream there are several options, as posted in: Retrofit callback get response body, the easiest being:

要读取TypedInputStream,有几个选项,如发布于:Retrofit callback get response body,最简单的方法是:

String body = new String(((TypedByteArray) response.getBody()).getBytes());

If the following error is thrown:

如果抛出以下错误:

java.lang.ClassCastException: retrofit.client.UrlConnectionClient$TypedInputStream cannot be cast to retrofit.mime.TypedByteArray

Then make sure that you set .setLogLevel(RestAdapter.LogLevel.FULL) on the RestAdapter that you use to create the service.

然后确保在用于创建服务的RestAdapter上设置.setLogLevel(RestAdapter.LogLevel.FULL)。