gpt4 book ai didi

java - Volley 响应无法将数据存储到对象java android

转载 作者:行者123 更新时间:2023-11-29 04:17:22 24 4
gpt4 key购买 nike

我的代码 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;}

}

最佳答案

显示错误信息是因为您在联系人上设置了位置,也在位置上设置了位置。

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

改为

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

关于java - Volley 响应无法将数据存储到对象java android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51491747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com