gpt4 book ai didi

java - 安卓 : getting multiple data in listview from server in another screen using Json

转载 作者:行者123 更新时间:2023-11-30 04:08:12 24 4
gpt4 key购买 nike

我正在尝试将 jsonobject 放入下一个屏幕 ListView 中。我可以在 ListView 中获取 1 个值,但我有多个值要获取。我能怎么做。

这是我从服务器获取字符串的代码:-

 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("Get_Friend_List", holder
.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
StringEntity se = new StringEntity(holder.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
resp = response.toString();
String t = EntityUtils.toString(response.getEntity());

try {
JSONArray get_string1 = new JSONArray(t);
JSONObject get_string = null;

// Receive the JSON object from server
String userid = (String) get_string.get("UserId");
String totalusers = (String) get_string.get("TotalUsers");
String SessionID = (String) get_string.get("SessionID");
for (int i = 0; i < get_string1.length(); i++) {
get_string = get_string1.getJSONObject(i);
JSONObject contact = (JSONObject) get_string
.get("contacts-" + i);

String contact_id = (String) contact.get("UserId");

contact_username = (String) contact.get("UserName");
contact_status = (String) contact.get("Status");
Contacts.setStatus(contact_status, i);
Contacts.setUserName(contact_username, i);
}

} catch (JSONException e) {
e.printStackTrace();
System.out.println("--error--" + e.getMessage());
}

} catch (ClientProtocolException e) {
Log.e(TAG, e.toString());
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}

这是我收到的来自服务器的响应。 但仅在 ListView 中存储最后一个值。

 Response from Server----{
"UserId": "1",
"TotalUsers": "4",
"contacts-0": {
"UserId": "3",
"UserName": "kumar",
"Status": "1"
},
"contacts-1": {
"UserId": "2",
"UserName": "rahul",
"Status": "1"
},
"contacts-2": {
"UserId": "4",
"UserName": "vlk",
"Status": "1"
},
"contacts-3": {
"UserId": "5",
"UserName": "vlk",
"Status": "1"
},
"SessionID": "39355"
}

联系人.java

public class Contacts {
public static String[] status = new String[100];

public static String[] usrname = new String[100];

public static String getStatus(int i) {
return status[i];
}

public static String getUserName(int i) {
return usrname[i];
}

public static void setStatus(String status, int i) {
Contacts.status[i] = status;
}

public static void setUserName(String username, int i) {
Contacts.usrname[i] = username;
}

}

最佳答案

json 字符串没有数组类型。您需要调用 has()方法(测试 json 属性是否存在),然后再阅读 contacts目的。另一个问题在 Contacts类型。您应该必须定义 Contact输入并初始化 List<Contact>存储一个或多个联系人。

看看联系人类

public class Contact
{
private String userId;
private String userName;
private String status;

public Contact() {
userId="";
userName="";
status="";
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getUserId() {
return userId;
}

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

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

@Override
public String toString() {
return "Contact{" + "userId=" + userId + ", userName=" + userName + ", status=" + status + '}';
}

}

以及读取/解析的代码json字符串。

 JSONObject get_string = new JSONObject(t);

String userid = (String) get_string.get("UserId");
String totalusers = (String) get_string.get("TotalUsers");
String SessionID = (String) get_string.get("SessionID");

int i=0;
JSONObject obj=null;

/* Initialize the List<Contact> */
List<Contact> list=new ArrayList();

while(get_string.has("contacts-"+i))
{
obj=get_string.getJSONObject("contacts-" + i);
Contact contact=new Contact();
contact.setUserId(obj.getString("UserId"));
contact.setUserName(obj.getString("UserName"));
contact.setStatus(obj.getString("Status"));
list.add(contact);
i++;
}

关于java - 安卓 : getting multiple data in listview from server in another screen using Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11289760/

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