改造 returns 空对象,但 json 似乎有效
Retrofit returns empty object, but json seems valid
这是我从端点获得的 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);
最后是我的模型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;
}
为什么要改造 return 我一个空的用户对象? Json 有效,不会抛出任何错误。
给定模拟 API,其中 returns 你的 JSON: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
这是我从端点获得的 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);
最后是我的模型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;
}
为什么要改造 return 我一个空的用户对象? Json 有效,不会抛出任何错误。
给定模拟 API,其中 returns 你的 JSON: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