嗨,我已经开始使用新库 Loopj 和 GSON 进行 Web 服务响应。
但是我无法从 GSON 到 POJO 获得价值。我无法理解如何从 gson 中获取价值....
提前谢谢你。
我已经尝试捕获它会给我空指针异常
这是我的json解析
{
"user": {
"u_id": "89",
"u_name": "Vasudev",
"u_email": "dec@mail.com",
"u_mno": "9638063647",
"u_pass": "43914d8bde432de32b78b28b4cd8110e",
"add_by": "0",
"u_type": "owner",
"veri_code": "20-08-2015 12:34:18",
"created_at": "",
"profile_img": "no",
"status": "Hi There",
"device_id": "",
"push_token": "",
"gcm_id": "APA91bF85pjEQpR4vBDc-xzas2EIGtd77N218qMMrlnSauJOMI-HLAbSjPu7jroMLjfsGCLlSr_lM5XclQW84sbbLYeZA6-9Nt7Y5EFw7zvxIs_sOQ1jCggdBmPfgINdAjwZwOG4Pge0",
"active": "a",
"country": "India"
},
"msg": {
"message": "Success...!",
"status": "1"
}
}
在这里我的对象类得到更多的想法......
package objects;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class LoginInfo {
@SerializedName("LoginInfo")
public List<LoginInfo> LogIn;
@SerializedName("u_id")
public String U_ID;
@SerializedName("u_name")
public String U_NAME;
@SerializedName("u_email")
public String U_EMAIL;
@SerializedName("u_mno")
public String U_MNO;
@SerializedName("u_pass")
public String U_PASS;
@SerializedName("add_by")
public String ADD_BY;
@SerializedName("u_type")
public String U_TYPE;
@SerializedName("veri_code")
public String VERI_CODE;
@SerializedName("created_at")
public String CREATED_AT;
@SerializedName("profile_img")
public String PROFILE_IMG;
@SerializedName("status")
public String STATUS;
@SerializedName("gcm_id")
public String GCM_ID;
@SerializedName("active")
public String ACTIVE;
@SerializedName("country")
public String COUNTRY;
public List<LoginInfo> getLogIn() {
return LogIn;
}
public void setLogIn(List<LoginInfo> logIn) {
LogIn = logIn;
}
public String getU_ID() {
return U_ID;
}
public void setU_ID(String u_ID) {
U_ID = u_ID;
}
public String getU_NAME() {
return U_NAME;
}
public void setU_NAME(String u_NAME) {
U_NAME = u_NAME;
}
public String getU_EMAIL() {
return U_EMAIL;
}
public void setU_EMAIL(String u_EMAIL) {
U_EMAIL = u_EMAIL;
}
public String getU_MNO() {
return U_MNO;
}
public void setU_MNO(String u_MNO) {
U_MNO = u_MNO;
}
public String getU_PASS() {
return U_PASS;
}
public void setU_PASS(String u_PASS) {
U_PASS = u_PASS;
}
public String getADD_BY() {
return ADD_BY;
}
public void setADD_BY(String ADD_BY) {
this.ADD_BY = ADD_BY;
}
public String getU_TYPE() {
return U_TYPE;
}
public void setU_TYPE(String u_TYPE) {
U_TYPE = u_TYPE;
}
public String getVERI_CODE() {
return VERI_CODE;
}
public void setVERI_CODE(String VERI_CODE) {
this.VERI_CODE = VERI_CODE;
}
public String getCREATED_AT() {
return CREATED_AT;
}
public void setCREATED_AT(String CREATED_AT) {
this.CREATED_AT = CREATED_AT;
}
public String getPROFILE_IMG() {
return PROFILE_IMG;
}
public void setPROFILE_IMG(String PROFILE_IMG) {
this.PROFILE_IMG = PROFILE_IMG;
}
public String getSTATUS() {
return STATUS;
}
public void setSTATUS(String STATUS) {
this.STATUS = STATUS;
}
public String getGCM_ID() {
return GCM_ID;
}
public void setGCM_ID(String GCM_ID) {
this.GCM_ID = GCM_ID;
}
public String getACTIVE() {
return ACTIVE;
}
public void setACTIVE(String ACTIVE) {
this.ACTIVE = ACTIVE;
}
public String getCOUNTRY() {
return COUNTRY;
}
public void setCOUNTRY(String COUNTRY) {
this.COUNTRY = COUNTRY;
}
}
这是我的班级,我试图从 GSON 获得价值
public void getLogin() {
RequestParams params = new RequestParams();
params.put("user_name", strUserName);
params.put("user_pass", strPassword);
pd = ProgressDialog.show(LoginActivity.this, "", "Loading...", true, false);
AsyncHttpClient mClient = new AsyncHttpClient();
mClient.setTimeout(6 * 1000);
mClient.post( mURL+"login.php", params, new LoginResponsHandler());
}
class LoginResponsHandler extends AsyncHttpResponseHandler {
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
public void onSuccess(String content) {
// TODO Auto-generated method stub
super.onSuccess(content);
try {
LogsClass.ToastMsg(getApplicationContext(), content);
loginInfo = new LoginInfo();
Gson gson = new Gson();
Type infoType = new TypeToken<LoginInfo>() {}.getType();
Log.e("response", content);
loginInfo = gson.fromJson(content, infoType);
String Name = loginInfo.U_NAME;
String homephone = loginInfo.U_MNO;
String Name1 = loginInfo.getU_NAME();
String homephone1 = loginInfo.getU_MNO();
String Name2 = loginInfo.LogIn.get(0).U_NAME;
String homephone2 = loginInfo.LogIn.get(0).getU_MNO();
} catch (Exception ex) {
LogsClass.e("--->" + ex.getMessage());
}
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
super.onFinish();
pd.dismiss();
}
@Override
public void onFailure(Throwable error, String content) {
// TODO Auto-generated method stub
super.onFailure(error, content);
LogsClass.ToastMsg(getApplicationContext(), "Failure");
pd.dismiss();
}
}