我使用GSON解析服务器的响应。但是,得到
com.google.gson.JsonSyntaxException
目前我正在发送硬编码的JSON。删除了所有布尔值,浮点值等。目前我只解析字符串值。
LoginPOJO.java
package com.chsoneresidential.chsone.pojo;
import com.google.gson.annotations.SerializedName;
/**
* Created by pawan on 24/10/16.
*/
public class LoginPOJO {
//High priority variables goes below.....
@SerializedName("version")
private String version;
@SerializedName("app")
private String app;
// @SerializedName("error")
// private String error;
@SerializedName("status")
private String status;
@SerializedName("message")
private String message;
@SerializedName("access_token")
private String access_token;
@SerializedName("token_type")
private String token_type;
// @SerializedName("expires")
// private String expires;
// @SerializedName("expires_in")
// private String expires_in;
@SerializedName("refresh_token")
private String refresh_token;
@SerializedName("user_id")
private String user_id;
@SerializedName("role")
private String role;
@SerializedName("user_name")
private String user_name;
@SerializedName("soc_id")
private String soc_id;
@SerializedName("user_first_name")
private String user_first_name;
@SerializedName("user_last_name")
private String user_last_name;
@SerializedName("user_email_id")
private String user_email_id;
@SerializedName("user_mobile_number")
private String user_mobile_number;
@SerializedName("user_dob")
private String user_dob;
@SerializedName("user_profile_photo")
private String user_profile_photo;
}//LoginPOJO closes here....
我的活动,我得到回应:
@Override
public void networkResponseSuccess(String response) {
Log.d(TAG, "networkResponseSuccess: "+response);
try{
Gson gson = new Gson();
// LoginPOJO loginPOJO = gson.fromJson(reader, Staff.class);
LoginPOJO loginPOJO = gson.fromJson("{\n" +
"\t\"app\": {\n" +
"\t\t\"version\": \"v1.0\",\n" +
"\t\t\"app\": \"CHSONE\"\n" +
"\t},\n" +
"\t\"status\": \"OK\",\n" +
"\t\"message\": \"Welcome\",\n" +
"\t\"data\": {\n" +
"\t\t\"access_info\": {\n" +
"\t\t\t\"access_token\": \"OWcbj51uCe2SLQt7ad3XjUKRUawWMoUSvjSLPMVJ\",\n" +
"\t\t\t\"token_type\": \"Bearer\",\n" +
"\t\t\t\"refresh_token\": \"Bto2r5LkGdHNsYydUzi1Zj02aUl3J1CazZJ2g3F0\"\n" +
"\t\t},\n" +
"\t\t\"user_data\": {\n" +
"\t\t\t\"user_id\": \"803\",\n" +
"\t\t\t\"role\": \"admin,member\",\n" +
"\t\t\t\"user_name\": \"virajk123456\",\n" +
"\t\t\t\"soc_id\": \"846\",\n" +
"\t\t\t\"user_first_name\": \"Viraj\",\n" +
"\t\t\t\"user_last_name\": \"kaulkar\",\n" +
"\t\t\t\"user_email_id\": \"viraj@futurescape.co.in\",\n" +
"\t\t\t\"user_mobile_number\": \"8828162613\",\n" +
"\t\t\t\"user_dob\": \"1987-09-10\",\n" +
"\t\t\t\"user_profile_photo\": \"dev.chsone.in.s3.amazonaws.com\\/846\\/avatar\\/803\\/avtar_803.jpg\"\n" +
"\t\t}\n" +
"\t}\n" +
"}", LoginPOJO.class);
System.out.println(loginPOJO);
}//try closes here....
catch(Exception e){
e.printStackTrace();
Log.e(TAG, "networkResponseSuccess: Exception = "+e);
}//catch closes here.....
LOGCAT在哪里获取JSON语法异常
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 2 column 10 path $.app com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at com.google.gson.Gson.fromJson(Gson.java:887)
at com.google.gson.Gson.fromJson(Gson.java:852)
at com.google.gson.Gson.fromJson(Gson.java:801)
at com.google.gson.Gson.fromJson(Gson.java:773)
at com.chsoneresidential.chsone.LoginActivity.networkResponseSuccess(LoginActivity.java:166)
at com.chsoneresidential.chsone.network.NetworkResponseHandler.onResponse(NetworkResponseHandler.java:139)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 2 column 10 path $.app
at com.google.gson.stream.JsonReader.nextString(JsonReader.java:825)
at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:418)
at com.google.gson.internal.bind.TypeAdapters$16.read(TypeAdapters.java:406)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
... 18 more
答案 0 :(得分:0)
使用以下代码解析你的json:
public class Example {
@SerializedName("app")
@Expose
private App app;
@SerializedName("status")
@Expose
private String status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private Data data;
/**
*
* @return
* The app
*/
public App getApp() {
return app;
}
/**
*
* @param app
* The app
*/
public void setApp(App app) {
this.app = app;
}
/**
*
* @return
* The status
*/
public String getStatus() {
return status;
}
/**
*
* @param status
* The status
*/
public void setStatus(String status) {
this.status = status;
}
/**
*
* @return
* The message
*/
public String getMessage() {
return message;
}
/**
*
* @param message
* The message
*/
public void setMessage(String message) {
this.message = message;
}
/**
*
* @return
* The data
*/
public Data getData() {
return data;
}
/**
*
* @param data
* The data
*/
public void setData(Data data) {
this.data = data;
}
}
public class App {
@SerializedName("version")
@Expose
private String version;
@SerializedName("app")
@Expose
private String app;
/**
*
* @return
* The version
*/
public String getVersion() {
return version;
}
/**
*
* @param version
* The version
*/
public void setVersion(String version) {
this.version = version;
}
/**
*
* @return
* The app
*/
public String getApp() {
return app;
}
/**
*
* @param app
* The app
*/
public void setApp(String app) {
this.app = app;
}
}
public class Data {
@SerializedName("access_info")
@Expose
private AccessInfo accessInfo;
@SerializedName("user_data")
@Expose
private UserData userData;
/**
*
* @return
* The accessInfo
*/
public AccessInfo getAccessInfo() {
return accessInfo;
}
/**
*
* @param accessInfo
* The access_info
*/
public void setAccessInfo(AccessInfo accessInfo) {
this.accessInfo = accessInfo;
}
/**
*
* @return
* The userData
*/
public UserData getUserData() {
return userData;
}
/**
*
* @param userData
* The user_data
*/
public void setUserData(UserData userData) {
this.userData = userData;
}
}
public class AccessInfo {
@SerializedName("access_token")
@Expose
private String accessToken;
@SerializedName("token_type")
@Expose
private String tokenType;
@SerializedName("refresh_token")
@Expose
private String refreshToken;
/**
*
* @return
* The accessToken
*/
public String getAccessToken() {
return accessToken;
}
/**
*
* @param accessToken
* The access_token
*/
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
/**
*
* @return
* The tokenType
*/
public String getTokenType() {
return tokenType;
}
/**
*
* @param tokenType
* The token_type
*/
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
/**
*
* @return
* The refreshToken
*/
public String getRefreshToken() {
return refreshToken;
}
/**
*
* @param refreshToken
* The refresh_token
*/
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
}
public class UserData {
@SerializedName("user_id")
@Expose
private String userId;
@SerializedName("role")
@Expose
private String role;
@SerializedName("user_name")
@Expose
private String userName;
@SerializedName("soc_id")
@Expose
private String socId;
@SerializedName("user_first_name")
@Expose
private String userFirstName;
@SerializedName("user_last_name")
@Expose
private String userLastName;
@SerializedName("user_email_id")
@Expose
private String userEmailId;
@SerializedName("user_mobile_number")
@Expose
private String userMobileNumber;
@SerializedName("user_dob")
@Expose
private String userDob;
@SerializedName("user_profile_photo")
@Expose
private String userProfilePhoto;
/**
*
* @return
* The userId
*/
public String getUserId() {
return userId;
}
/**
*
* @param userId
* The user_id
*/
public void setUserId(String userId) {
this.userId = userId;
}
/**
*
* @return
* The role
*/
public String getRole() {
return role;
}
/**
*
* @param role
* The role
*/
public void setRole(String role) {
this.role = role;
}
/**
*
* @return
* The userName
*/
public String getUserName() {
return userName;
}
/**
*
* @param userName
* The user_name
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
*
* @return
* The socId
*/
public String getSocId() {
return socId;
}
/**
*
* @param socId
* The soc_id
*/
public void setSocId(String socId) {
this.socId = socId;
}
/**
*
* @return
* The userFirstName
*/
public String getUserFirstName() {
return userFirstName;
}
/**
*
* @param userFirstName
* The user_first_name
*/
public void setUserFirstName(String userFirstName) {
this.userFirstName = userFirstName;
}
/**
*
* @return
* The userLastName
*/
public String getUserLastName() {
return userLastName;
}
/**
*
* @param userLastName
* The user_last_name
*/
public void setUserLastName(String userLastName) {
this.userLastName = userLastName;
}
/**
*
* @return
* The userEmailId
*/
public String getUserEmailId() {
return userEmailId;
}
/**
*
* @param userEmailId
* The user_email_id
*/
public void setUserEmailId(String userEmailId) {
this.userEmailId = userEmailId;
}
/**
*
* @return
* The userMobileNumber
*/
public String getUserMobileNumber() {
return userMobileNumber;
}
/**
*
* @param userMobileNumber
* The user_mobile_number
*/
public void setUserMobileNumber(String userMobileNumber) {
this.userMobileNumber = userMobileNumber;
}
/**
*
* @return
* The userDob
*/
public String getUserDob() {
return userDob;
}
/**
*
* @param userDob
* The user_dob
*/
public void setUserDob(String userDob) {
this.userDob = userDob;
}
/**
*
* @return
* The userProfilePhoto
*/
public String getUserProfilePhoto() {
return userProfilePhoto;
}
/**
*
* @param userProfilePhoto
* The user_profile_photo
*/
public void setUserProfilePhoto(String userProfilePhoto) {
this.userProfilePhoto = userProfilePhoto;
}
}