-2

嗨,我已经开始使用新库 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();
    }
}
4

2 回答 2

0

将您的对象类更改为

public class LoginInfo {
  UserInfo user;
  MessageInfo msg;
  //getter and setter for them
}

并分别使用键作为字符串变量创建类 UserInfo 和 MessageInfo

并进行以下更改

String Name =  loginInfo.getUser().getU_Name();
于 2015-08-22T11:21:00.943 回答
0

在这里,我从上面的示例中找出了我的问题,并进行了一些更改。

这又是我的 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"
}

正确的对象类别:

  public class LoginInfo {


@SerializedName("user")
public UserData UserInfo;

public class UserData {

    @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;


}


@SerializedName("msg")
public MessageData MessageInfo;

public class MessageData
{


    @SerializedName("message")
    public String Messsage;

    @SerializedName("status")
    public int Status;

}

}

这是我获得价值的活动课程:

    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(Urls.BASE_URL, 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 {


            loginInfo = new LoginInfo();
            Gson gson = new Gson();
            Type infoType = new TypeToken<LoginInfo>() {}.getType();
            Log.e("response", content);
            loginInfo = gson.fromJson(content, infoType);




            if(loginInfo.MessageInfo.Status==1)
            {

                Toast.makeText(LoginActivity.this,""+loginInfo.MessageInfo.Messsage,Toast.LENGTH_SHORT).show();;
                String name=loginInfo.UserInfo.U_NAME;
                String id=loginInfo.UserInfo.U_ID;


                LogsClass.ToastMsg(getApplicationContext(),loginInfo.UserInfo.U_NAME + "n/"
                            + loginInfo.UserInfo.U_MNO);

                Utils.saveToUserDefaults(LoginActivity.this, Constants.KEY_UID, loginInfo.UserInfo.U_ID);
                Utils.saveToUserDefaults(LoginActivity.this,Constants.KEY_UNAME,loginInfo.UserInfo.U_NAME);
                Utils.saveToUserDefaults(LoginActivity.this,Constants.KEY_EMAILID,loginInfo.UserInfo.U_EMAIL);
                Utils.saveToUserDefaults(LoginActivity.this,Constants.KEY_NUMBER,loginInfo.UserInfo.U_MNO);
                Utils.saveToUserDefaults(LoginActivity.this,Constants.KEY_PASSWORD,loginInfo.UserInfo.U_PASS);



            }else
            {


            }


        } 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();
    }
}
于 2015-08-24T06:01:27.597 回答