我正在使用Retrofit和Gson Converter,但我得到了这个奇怪的异常。我使用两个URL来执行调用。在这两种情况下,我都执行相同的调用并在完整日志级别获得相同的响应,但其中一个URL给了我这个例外,我无法在第二种情况下解析数据。
使用Retrofit v1.9和Gson v2.6.1。
谢谢!
错误:
E/UserCommunicator: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: Expecting character, got:
retrofit.RetrofitError: com.google.gson.JsonSyntaxException: Expecting character, got:
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:378)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:818)
Caused by: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: Expecting character, got:
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:67)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:362)
... 7 more
Caused by: com.google.gson.JsonSyntaxException: Expecting character, got:
at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:397)
at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:388)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:116)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:116)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216)
at com.google.gson.Gson.fromJson(Gson.java:879)
at com.google.gson.Gson.fromJson(Gson.java:844)
at retrofit.converter.GsonConverter.fromBody(GsonConverter.java:63)
... 8 more
E/LoginActivity: com.google.gson.JsonSyntaxException: Expecting character, got:
来自服务器的Json:
{
"user_data": {
"user_id": "4",
"gid": "",
"gname": "",
"gphoto": "",
"fid": "",
"fname": "",
"fphoto": "",
"name": "John",
"surname": "Doe",
"email": "johndoe@example.com",
"birthday": null,
"registered": null,
"username": null,
"user_type": "a",
"rnd": "xxxxxxxxxxx",
"user_lang": "en",
"clinic_name": null,
"clinic_id": null,
"admin": null,
"state": null,
"gender": "",
"cel": "60000000"
},
"clinic_data": {
"clinic_name": "Doe",
"clinic_id": "2",
"plan": "plan",
"mod_finance": "0",
"mod_surgery": "0",
"mod_ortodoncy": "0",
"staff_type": "staff_type",
"admin": "0",
"state": "state",
"address": "address",
"latitude": "0.0",
"longitude": "0.0",
"cel": "",
"nr_licenses": null,
"expires": null
},
"response": "Success"
}
答案 0 :(得分:0)
你的GSON对象可能是错误的。你可以generate Java classes based on your JSON for GSON。
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ClinicData {
@SerializedName("clinic_name")
@Expose
private String clinicName;
@SerializedName("clinic_id")
@Expose
private String clinicId;
@SerializedName("plan")
@Expose
private String plan;
@SerializedName("mod_finance")
@Expose
private String modFinance;
@SerializedName("mod_surgery")
@Expose
private String modSurgery;
@SerializedName("mod_ortodoncy")
@Expose
private String modOrtodoncy;
@SerializedName("staff_type")
@Expose
private String staffType;
@SerializedName("admin")
@Expose
private String admin;
@SerializedName("state")
@Expose
private String state;
@SerializedName("address")
@Expose
private String address;
@SerializedName("latitude")
@Expose
private String latitude;
@SerializedName("longitude")
@Expose
private String longitude;
@SerializedName("cel")
@Expose
private String cel;
@SerializedName("nr_licenses")
@Expose
private Object nrLicenses;
@SerializedName("expires")
@Expose
private Object expires;
/**
*
* @return
* The clinicName
*/
public String getClinicName() {
return clinicName;
}
/**
*
* @param clinicName
* The clinic_name
*/
public void setClinicName(String clinicName) {
this.clinicName = clinicName;
}
/**
*
* @return
* The clinicId
*/
public String getClinicId() {
return clinicId;
}
/**
*
* @param clinicId
* The clinic_id
*/
public void setClinicId(String clinicId) {
this.clinicId = clinicId;
}
/**
*
* @return
* The plan
*/
public String getPlan() {
return plan;
}
/**
*
* @param plan
* The plan
*/
public void setPlan(String plan) {
this.plan = plan;
}
/**
*
* @return
* The modFinance
*/
public String getModFinance() {
return modFinance;
}
/**
*
* @param modFinance
* The mod_finance
*/
public void setModFinance(String modFinance) {
this.modFinance = modFinance;
}
/**
*
* @return
* The modSurgery
*/
public String getModSurgery() {
return modSurgery;
}
/**
*
* @param modSurgery
* The mod_surgery
*/
public void setModSurgery(String modSurgery) {
this.modSurgery = modSurgery;
}
/**
*
* @return
* The modOrtodoncy
*/
public String getModOrtodoncy() {
return modOrtodoncy;
}
/**
*
* @param modOrtodoncy
* The mod_ortodoncy
*/
public void setModOrtodoncy(String modOrtodoncy) {
this.modOrtodoncy = modOrtodoncy;
}
/**
*
* @return
* The staffType
*/
public String getStaffType() {
return staffType;
}
/**
*
* @param staffType
* The staff_type
*/
public void setStaffType(String staffType) {
this.staffType = staffType;
}
/**
*
* @return
* The admin
*/
public String getAdmin() {
return admin;
}
/**
*
* @param admin
* The admin
*/
public void setAdmin(String admin) {
this.admin = admin;
}
/**
*
* @return
* The state
*/
public String getState() {
return state;
}
/**
*
* @param state
* The state
*/
public void setState(String state) {
this.state = state;
}
/**
*
* @return
* The address
*/
public String getAddress() {
return address;
}
/**
*
* @param address
* The address
*/
public void setAddress(String address) {
this.address = address;
}
/**
*
* @return
* The latitude
*/
public String getLatitude() {
return latitude;
}
/**
*
* @param latitude
* The latitude
*/
public void setLatitude(String latitude) {
this.latitude = latitude;
}
/**
*
* @return
* The longitude
*/
public String getLongitude() {
return longitude;
}
/**
*
* @param longitude
* The longitude
*/
public void setLongitude(String longitude) {
this.longitude = longitude;
}
/**
*
* @return
* The cel
*/
public String getCel() {
return cel;
}
/**
*
* @param cel
* The cel
*/
public void setCel(String cel) {
this.cel = cel;
}
/**
*
* @return
* The nrLicenses
*/
public Object getNrLicenses() {
return nrLicenses;
}
/**
*
* @param nrLicenses
* The nr_licenses
*/
public void setNrLicenses(Object nrLicenses) {
this.nrLicenses = nrLicenses;
}
/**
*
* @return
* The expires
*/
public Object getExpires() {
return expires;
}
/**
*
* @param expires
* The expires
*/
public void setExpires(Object expires) {
this.expires = expires;
}
}
@Generated("org.jsonschema2pojo")
public class Example {
@SerializedName("user_data")
@Expose
private UserData userData;
@SerializedName("clinic_data")
@Expose
private ClinicData clinicData;
@SerializedName("response")
@Expose
private String response;
/**
*
* @return
* The userData
*/
public UserData getUserData() {
return userData;
}
/**
*
* @param userData
* The user_data
*/
public void setUserData(UserData userData) {
this.userData = userData;
}
/**
*
* @return
* The clinicData
*/
public ClinicData getClinicData() {
return clinicData;
}
/**
*
* @param clinicData
* The clinic_data
*/
public void setClinicData(ClinicData clinicData) {
this.clinicData = clinicData;
}
/**
*
* @return
* The response
*/
public String getResponse() {
return response;
}
/**
*
* @param response
* The response
*/
public void setResponse(String response) {
this.response = response;
}
}
public class UserData {
@SerializedName("user_id")
@Expose
private String userId;
@SerializedName("gid")
@Expose
private String gid;
@SerializedName("gname")
@Expose
private String gname;
@SerializedName("gphoto")
@Expose
private String gphoto;
@SerializedName("fid")
@Expose
private String fid;
@SerializedName("fname")
@Expose
private String fname;
@SerializedName("fphoto")
@Expose
private String fphoto;
@SerializedName("name")
@Expose
private String name;
@SerializedName("surname")
@Expose
private String surname;
@SerializedName("email")
@Expose
private String email;
@SerializedName("birthday")
@Expose
private Object birthday;
@SerializedName("registered")
@Expose
private Object registered;
@SerializedName("username")
@Expose
private Object username;
@SerializedName("user_type")
@Expose
private String userType;
@SerializedName("rnd")
@Expose
private String rnd;
@SerializedName("user_lang")
@Expose
private String userLang;
@SerializedName("clinic_name")
@Expose
private Object clinicName;
@SerializedName("clinic_id")
@Expose
private Object clinicId;
@SerializedName("admin")
@Expose
private Object admin;
@SerializedName("state")
@Expose
private Object state;
@SerializedName("gender")
@Expose
private String gender;
@SerializedName("cel")
@Expose
private String cel;
/**
*
* @return
* The userId
*/
public String getUserId() {
return userId;
}
/**
*
* @param userId
* The user_id
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
*
* @return
* The gid
*/
public String getGid() {
return gid;
}
/**
*
* @param gid
* The gid
*/
public void setGid(String gid) {
this.gid = gid;
}
/**
*
* @return
* The gname
*/
public String getGname() {
return gname;
}
/**
*
* @param gname
* The gname
*/
public void setGname(String gname) {
this.gname = gname;
}
/**
*
* @return
* The gphoto
*/
public String getGphoto() {
return gphoto;
}
/**
*
* @param gphoto
* The gphoto
*/
public void setGphoto(String gphoto) {
this.gphoto = gphoto;
}
/**
*
* @return
* The fid
*/
public String getFid() {
return fid;
}
/**
*
* @param fid
* The fid
*/
public void setFid(String fid) {
this.fid = fid;
}
/**
*
* @return
* The fname
*/
public String getFname() {
return fname;
}
/**
*
* @param fname
* The fname
*/
public void setFname(String fname) {
this.fname = fname;
}
/**
*
* @return
* The fphoto
*/
public String getFphoto() {
return fphoto;
}
/**
*
* @param fphoto
* The fphoto
*/
public void setFphoto(String fphoto) {
this.fphoto = fphoto;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The surname
*/
public String getSurname() {
return surname;
}
/**
*
* @param surname
* The surname
*/
public void setSurname(String surname) {
this.surname = surname;
}
/**
*
* @return
* The email
*/
public String getEmail() {
return email;
}
/**
*
* @param email
* The email
*/
public void setEmail(String email) {
this.email = email;
}
/**
*
* @return
* The birthday
*/
public Object getBirthday() {
return birthday;
}
/**
*
* @param birthday
* The birthday
*/
public void setBirthday(Object birthday) {
this.birthday = birthday;
}
/**
*
* @return
* The registered
*/
public Object getRegistered() {
return registered;
}
/**
*
* @param registered
* The registered
*/
public void setRegistered(Object registered) {
this.registered = registered;
}
/**
*
* @return
* The username
*/
public Object getUsername() {
return username;
}
/**
*
* @param username
* The username
*/
public void setUsername(Object username) {
this.username = username;
}
/**
*
* @return
* The userType
*/
public String getUserType() {
return userType;
}
/**
*
* @param userType
* The user_type
*/
public void setUserType(String userType) {
this.userType = userType;
}
/**
*
* @return
* The rnd
*/
public String getRnd() {
return rnd;
}
/**
*
* @param rnd
* The rnd
*/
public void setRnd(String rnd) {
this.rnd = rnd;
}
/**
*
* @return
* The userLang
*/
public String getUserLang() {
return userLang;
}
/**
*
* @param userLang
* The user_lang
*/
public void setUserLang(String userLang) {
this.userLang = userLang;
}
/**
*
* @return
* The clinicName
*/
public Object getClinicName() {
return clinicName;
}
/**
*
* @param clinicName
* The clinic_name
*/
public void setClinicName(Object clinicName) {
this.clinicName = clinicName;
}
/**
*
* @return
* The clinicId
*/
public Object getClinicId() {
return clinicId;
}
/**
*
* @param clinicId
* The clinic_id
*/
public void setClinicId(Object clinicId) {
this.clinicId = clinicId;
}
/**
*
* @return
* The admin
*/
public Object getAdmin() {
return admin;
}
/**
*
* @param admin
* The admin
*/
public void setAdmin(Object admin) {
this.admin = admin;
}
/**
*
* @return
* The state
*/
public Object getState() {
return state;
}
/**
*
* @param state
* The state
*/
public void setState(Object state) {
this.state = state;
}
/**
*
* @return
* The gender
*/
public String getGender() {
return gender;
}
/**
*
* @param gender
* The gender
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
*
* @return
* The cel
*/
public String getCel() {
return cel;
}
/**
*
* @param cel
* The cel
*/
public void setCel(String cel) {
this.cel = cel;
}
}