Volley Response无法将数据存储到对象Java android

时间:2018-07-24 06:21:06

标签: java android

我的代码可以100%工作。我不知道何时存储Volley onresponse的接收输出,我似乎无法正确地将它们存储到对象中。原始输出正确。当我将其存储到我的对象中,然后尝试读取该对象时,它显示了错误的信息。 例如,没有 contact 的数据,并且 position 的数据在错误的位置输出。

Log.d("TAG", "onResponse : "+ response.toString());
myProfile.set_firstname(response.optString("firstname", ""));
myProfile.set_lastname(response.optString("lastname", ""));
myProfile.set_contact(response.optString("contact", ""));
myProfile.set_email(response.optString("email", ""));
myProfile.set_position(response.optString("position", ""));
myProfile.set_areaname(response.optString("area", ""));
myProfile.set_deptname(response.optString("department", ""));
Log.d("TAG", "myProfile SET : " +
    "firstname: "+myProfile.get_firstname() + " " +
    "lastname: "+myProfile.get_lastname() + " " +
    "contact: "+myProfile.get_contact() + " " +
    "email: "+myProfile.get_email() + " " +
    "position: "+myProfile.get_position() + " " +
    "area: "+myProfile.get_areaname() + " " +
    "dept: "+myProfile.get_deptname()
);

Volley和Log.d的原始JSON响应

onResponse : {"firstname":"kirpal","lastname":"SINGH","contact":"0164028083","email":"kirpal@gmail.com","position":"Technician","area":"Nextrack","department":"nexpro"}

myProfile SET : firstname: kirpal lastname: SINGH contact: Technician email: kirpal@gmail.com position:  area: Nextrack dept: nexpro

MyProfile.java

public class MyProfile {
    private String id="";               
    private String firstname="";        
    private String lastname="";         
    private String areaname ="";        
    private String deptname ="";        
    private String contact ="";       
    private String email ="";       
    private String position ="";        

    //SET
    public void set_id(String id){this.id = id;}
    public void set_firstname(String firstname){this.firstname = firstname;}
    public void set_lastname(String lastname){this.lastname = lastname;}
    public void set_areaname(String areaname){this.areaname = areaname;}
    public void set_deptname(String deptname){this.deptname = deptname;}
    public void set_contact(String contact){this.contact = contact;}
    public void set_email(String email){this.email = email;}
    public void set_position(String position){this.contact = position;}

    //GET
    public String get_id(){return this.id;}
    public String get_firstname(){return this.firstname;}
    public String get_lastname(){return this.lastname;}
    public String get_areaname(){return this.areaname;}
    public String get_deptname(){return this.deptname;}
    public String get_contact(){return this.contact;}
    public String get_email(){return this.email;}
    public String get_position(){return this.position;}

}

1 个答案:

答案 0 :(得分:3)

显示错误信息,因为您在触点上设置了位置,并且还在位置上设置了位置。

public void set_position(String position){this.contact = position;}

更改为

public void set_position(String position){this.position = position;}