-2

Hi i have started use of new library Loopj and GSON for Web Service response.

However i am unable to get value from GSON to POJO. i couln't understand how to get value from the gson ....

thank you in adavance.

i have puted try catch it will give me null pointer exception

here my json parsing

 {
"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"
}

}

Here My Object Class to get more idea...

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

Here My Class where i m trying to get Value from 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();
    }
}
Vasudev Vyas
  • 726
  • 1
  • 10
  • 28
  • please add the JSON string to question and, also mention exactly what error or exception you are getting when tried the above snippet – Shadow Droid Aug 22 '15 at 10:54
  • @ShadowDroid i have posted my json .... it will giveme in try catch null pointer exception – Vasudev Vyas Aug 22 '15 at 11:13
  • you are getting null pointer because class you created for Serializing and Deserializing is not correct...Infirst place there on jsonObject of login – Shadow Droid Aug 22 '15 at 11:20

2 Answers2

0

change your object class to

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

and create class UserInfo and MessageInfo with keys as string variables respectively

and make following changes

String Name =  loginInfo.getUser().getU_Name();
Shadow Droid
  • 1,696
  • 1
  • 12
  • 26
0

Here i am figure out my problem from above example with little bit change.

this my JSON Response post again:

{

"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"
}

Correct Object Class :

  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;

}

}

this my Activity class where i am getting value:

    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();
    }
}
Vasudev Vyas
  • 726
  • 1
  • 10
  • 28
  • it is working for you ryt? with you understanding the stuff and making neccessary changes from my small solution seems gr8...in first place if you had googled gson, you will find many examples and tutorial. And a small suggestion instead of accessing the instance variables directly try and use **getter methods** for more explanation : http://stackoverflow.com/questions/8830772/why-are-of-getter-and-setter-method-important-in-java – Shadow Droid Aug 24 '15 at 09:50