gpt4 book ai didi

android - Retrofit 返回空对象,但 json 似乎有效

转载 作者:行者123 更新时间:2023-11-30 02:04:16 25 4
gpt4 key购买 nike

这是我从端点获取的 json。我使用代理得到它,所以这正是改造得到的:

{
"id": 1,
"email": "jacek@gmail.com",
"name": "Jacek KwiecieŇĄ",
"google_plus_id": "117434793312604634191",
"facebook_id": null,
"avatar_url": "https://some_url",
"last_login_at": "2015-06-14T12:36:58.831Z",
"created_at": "2015-06-14T12:36:58.829Z",
}

我这样创建 RetrofitService:

private static final RestAdapter REST_ADAPTER = new RestAdapter.Builder()
.setEndpoint(BuildConfig.SERVER_URL)
.setConverter(new GsonConverter(new GsonBuilder().create()))
.build();

public static final Api API = REST_ADAPTER.create(Api.class);

最后是我的模型类:

public class User {
public long id;
public boolean active;
public String email;
public String googlePlusId;
public String facebookId;
public String name;
public String avatarUrl;
public String coverPhotoUrl;
}

为什么改造会返回一个空的用户对象? Json 有效,不会抛出任何错误。

最佳答案

给定返回 JSON 的模拟 API:http://www.mocky.io/v2/55867bf40258d3a80360db06

这个演示工作得很好:

import com.google.gson.annotations.SerializedName;
import retrofit.RestAdapter;
import retrofit.http.GET;

public class RetrofitDemo {

static class User {

public long id;
public boolean active;
public String email;
public String name;
@SerializedName("google_plus_id") public String googlePlusId;
@SerializedName("facebook_id") public String facebookId;
@SerializedName("avatar_url") public String avatarUrl;
@SerializedName("cover_photo_url") public String coverPhotoUrl;
@SerializedName("last_login_at") public String lastLoginAt;
@SerializedName("created_at") public String createdAt;

@Override
public String toString() {
return "User{" +
"id=" + id +
", active=" + active +
", email='" + email + '\'' +
", name='" + name + '\'' +
", googlePlusId='" + googlePlusId + '\'' +
", facebookId='" + facebookId + '\'' +
", avatarUrl='" + avatarUrl + '\'' +
", coverPhotoUrl='" + coverPhotoUrl + '\'' +
", lastLoginAt='" + lastLoginAt + '\'' +
", createdAt='" + createdAt + '\'' +
'}';
}
}

interface API {

@GET("/55867bf40258d3a80360db06")
User getUser();
}

public static void main(String[] args) {

API api = new RestAdapter.Builder()
.setEndpoint("http://www.mocky.io/v2")
.build()
.create(API.class);

System.out.println(api.getUser());
}
}

编辑

从日志中我看到您的服务器响应 Content-Type: application/json。尝试让它以特定编码响应,比如:Content-Type: application/json; charset=utf-8

关于android - Retrofit 返回空对象,但 json 似乎有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30962517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com