gpt4 book ai didi

java - 如何使用 asynctask json 添加 pojo

转载 作者:行者123 更新时间:2023-11-30 00:39:10 25 4
gpt4 key购买 nike

我想从 Pojo.class 添加,因为我想使用 Pojo 对象。但是 DataRetriever 有我的错误。 DataRetriever for example case1 id,name 从 Pojo.class getName,getId 添加。还有这个网站 https://jsonplaceholder.typicode.com/users

数据检索器.java

 private void jsonParser(String jsonString) {
ArrayList<Post> posts = new Gson().fromJson(jsonString,
new TypeToken<ArrayList<Post>>() {
}.getType());
int names_count, id;
String name;
try {
JSONArray jsonArray = new JSONArray(jsonString);
names_count = jsonArray.length();
if (!listValues.isEmpty())
listValues.clear();
if (!pictureValues.isEmpty())
pictureValues.clear();
for (int i = 0; i < names_count; i++) {
JSONObject array_items = jsonArray.getJSONObject(i);
ListValues jsonValues, pictureValue;
switch (type) {
case 1:
id = array_items.optInt(posts.getClass().getName());
name = array_items.optString(posts.getClass().getUserId());
jsonValues = new ListValues(id, name);
listValues.add(jsonValues);
break;
case 2:
id = array_items.optInt("userId");
name = array_items.optString("title");
if (id == recieved_id) {
jsonValues = new ListValues(id, name);
listValues.add(jsonValues);
}
break;
case 3:
id = array_items.optInt("albumId");
name = array_items.optString("title");
String pictureURL = array_items.getString("url");
if (id == recieved_id) {
jsonValues = new ListValues(id, name);
pictureValue = new ListValues(id, pictureURL);
listValues.add(jsonValues);
pictureValues.add(pictureValue);
}
break;
}
}
} catch (JSONException e) {
Log.d(TAG, e.getLocalizedMessage());
}
}

Post.java

public class Post implements Serializable {
@SerializedName("userId")
private int userId;
@SerializedName("name")
@Expose
public String name;

@SerializedName("id")
private int id;

@SerializedName("title")
private String title;

@SerializedName("body")
private String body;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}


public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getBody() {
return body;
}

public void setBody(String body) {
this.body = body;
}
}

我的错误: enter image description here

最佳答案

你做错了,因为你在 id 中分配了 name,这不是整数类型。

你在 getClass() 上调用 userId (getUserId()) 的第二件事是错误的

//Wrong
id = array_items.optInt(posts.getName());// for name
name = array_items.optString(posts.getUserId());// for user id

//Correct
id = array_items.optInt(posts.getUserId());
name = array_items.optInt(posts.getName());

修改你的for循环

 for (int i = 0; i < names_count; i++) {
Post post = posts.get(i);// add this line also
JSONObject array_items = jsonArray.getJSONObject(i);
ListValues jsonValues, pictureValue;
switch (type) {
case 1:
id = array_items.optInt(post.getUserId());
name = array_items.optInt(post.getName());//write these 2 lines

jsonValues = new ListValues(id, name);
listValues.add(jsonValues);
break;
case 2:
id = array_items.optInt("userId");
name = array_items.optString("title");
if (id == recieved_id) {
jsonValues = new ListValues(id, name);
listValues.add(jsonValues);
}
break;
case 3:
id = array_items.optInt("albumId");
name = array_items.optString("title");
String pictureURL = array_items.getString("url");
if (id == recieved_id) {
jsonValues = new ListValues(id, name);
pictureValue = new ListValues(id, pictureURL);
listValues.add(jsonValues);
pictureValues.add(pictureValue);
}
break;
}
}

//更新的答案

-----------------------------------com.example.Address.java-----------------------------------

package com.example;// your package name

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class Address {

@SerializedName("street")
@Expose
private String street;
@SerializedName("suite")
@Expose
private String suite;
@SerializedName("city")
@Expose
private String city;
@SerializedName("zipcode")
@Expose
private String zipcode;
@SerializedName("geo")
@Expose
private Geo geo;

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

public String getSuite() {
return suite;
}

public void setSuite(String suite) {
this.suite = suite;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getZipcode() {
return zipcode;
}

public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}

public Geo getGeo() {
return geo;
}

public void setGeo(Geo geo) {
this.geo = geo;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

}
-----------------------------------com.example.Company.java-----------------------------------

package com.example;// your package name

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class Company {

@SerializedName("name")
@Expose
private String name;
@SerializedName("catchPhrase")
@Expose
private String catchPhrase;
@SerializedName("bs")
@Expose
private String bs;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getCatchPhrase() {
return catchPhrase;
}

public void setCatchPhrase(String catchPhrase) {
this.catchPhrase = catchPhrase;
}

public String getBs() {
return bs;
}

public void setBs(String bs) {
this.bs = bs;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

}
-----------------------------------com.example.Geo.java-----------------------------------

package com.example;// your package name

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class Geo {

@SerializedName("lat")
@Expose
private String lat;
@SerializedName("lng")
@Expose
private String lng;

public String getLat() {
return lat;
}

public void setLat(String lat) {
this.lat = lat;
}

public String getLng() {
return lng;
}

public void setLng(String lng) {
this.lng = lng;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

}
-----------------------------------com.example.UserModel.java-----------------------------------

package com.example;// your package name

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class UserModel {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("username")
@Expose
private String username;
@SerializedName("email")
@Expose
private String email;
@SerializedName("address")
@Expose
private Address address;
@SerializedName("phone")
@Expose
private String phone;
@SerializedName("website")
@Expose
private String website;
@SerializedName("company")
@Expose
private Company company;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public Address getAddress() {
return address;
}

public void setAddress(Address address) {
this.address = address;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getWebsite() {
return website;
}

public void setWebsite(String website) {
this.website = website;
}

public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}

}

关于java - 如何使用 asynctask json 添加 pojo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42835322/

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