【发布时间】:2015-08-20 11:06:56
【问题描述】:
我是第一次尝试使用改造库,但遇到了问题。
这是我的 JSON:
{
"id":"15",
"email":"example@gmail.com",
"phone":null,
"password":"",
"login":"",
"userProfile":{
"lan_status":"",
"name":"example",
"sex":"0",
"second_name":"",
"date_of_birth":"0000-00-00 00:00:00"
}
}
这是我的改造代码:
String url = "https://example.com";
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(url)
.build();
mServerApi = restAdapter.create(ServerApi.class);
mServerApi.getUserInformation( new Callback <GetUserProfileInfo> () {
@Override
public void success(GetUserProfileInfo getUserProfileInfo, Response response) {
saveUserInfromationToDb(mActivity, getUserProfileInfo);
}
@Override
public void failure(RetrofitError error) {
}
});
public class GetUserProfileInfo {
@SerializedName("id")
public int mUserId;
@SerializedName("email")
public String mUserEmail;
@SerializedName("phone")
public String mUserPhone;
@SerializedName("password")
public String mUserPassword;
@SerializedName("login")
public String mUserLogin;
@SerializedName("userProfile")
public List<GetUserProfileInfo_2> userProfile;
}
public class GetUserProfileInfo_2 {
@SerializedName("lanStatus")
public int mLanStatus;
@SerializedName("firstName")
public String mFirstName;
@SerializedName("lastName")
public String mLastName;
@SerializedName("sex")
public int mSex;
}
但我遇到了这个问题: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_ARRAY 但在第 1 行第 533 列路径 $.userProfile 中是 BEGIN_OBJECT
很高兴得到任何帮助!
【问题讨论】:
-
你能发布你的 API URL 吗?
-
userProfile 不是列表/数组
-
尝试@SerializedName("userProfile") public GetUserProfileInfo_2 userProfile;而不是 @SerializedName("userProfile") public List
userProfile; -
一个好问题,不值得 2 票反对
标签: android json serialization retrofit