gpt4 book ai didi

php - android如何获取json数组到listview

转载 作者:行者123 更新时间:2023-11-30 04:06:52 26 4
gpt4 key购买 nike

http://hopscriber.com/log.php

this [{"uname":"faith"},{"uname":"losh"},{"uname":"loshi"},{"uname":"test"}]

我想把它放到 ListView 中,但我不知道该怎么做,教程中的方法我也不清楚。

希望有人能帮帮我...

谢谢

给出上述结果的 php

<?php
include "db_config.php";

$sql=mysql_query("SELECT`uname` FROM `user`");
while($row=mysql_fetch_assoc($sql)) $output[]=$row;
print(json_encode($output));
mysql_close();
?>

Activity

这是更正后的,但 ListView 仅显示 pwd 列....这是为什么呢?

    package hopscriber.com;


public class Menu extends Activity {

TextView result;
Intent intent;

// JSON Node names
private static final String TAG_Name = null;
private static final String TAG_Pass = null;

// contacts JSONArray
JSONArray contacts = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);

// Name of the User
result = (TextView) findViewById(R.id.result);
intent = getIntent();
String Naming = intent.getStringExtra("name1");
result.setText("Hey " + Naming);

ListView list = (ListView) findViewById(R.id.list);

try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 0);
HttpClient httpClient = new DefaultHttpClient(params);

// prepare the HTTP GET call
HttpGet httpget = new HttpGet("http://hopscriber.com/log.php");
// get the response entity
HttpEntity entity = httpClient.execute(httpget).getEntity();

if (entity != null) {
// get the response content as a string
String response = EntityUtils.toString(entity);
// consume the entity
entity.consumeContent();

// When HttpClient instance is no longer needed, shut down the
// connection manager to ensure immediate deallocation of all
// system resources
httpClient.getConnectionManager().shutdown();

// return the JSON response
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

JSONArray jsonArray = new JSONArray(response.trim());
if (jsonArray != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = (JSONObject) jsonArray.get(i);

HashMap<String, String> map = new HashMap<String, String>();

map.put(TAG_Name, object.getString("uname"));
map.put(TAG_Pass, object.getString("pwd"));

contactList.add(map);

}
}

ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.menu_list_row, new String[] { TAG_Name,
TAG_Pass }, new int[] { R.id.LR_Name,
R.id.LR_date });
list.setAdapter(adapter);
}
} catch (Exception e) {
e.printStackTrace();
}

// Launching new screen on Selecting Single ListItem
list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(), "Booyah", Toast.LENGTH_SHORT)
.show();
}
});
}

}

最佳答案

试试这个,我没有测试它,但我使用不同数据的相同代码并且它有效..

try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(params, 0);
HttpClient httpClient = new DefaultHttpClient(params);

//prepare the HTTP GET call
HttpGet httpget = new HttpGet(urlString);
//get the response entity
HttpEntity entity = httpClient.execute(httpget).getEntity();

if (entity != null) {
//get the response content as a string
String response = EntityUtils.toString(entity);
//consume the entity
entity.consumeContent();

// When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();

//return the JSON response
JSONArray jsonArray = new JSONArray(response.trim());
if(jsonArray != null) {
String[] unames = new String[jsonArray.length()];
for(int i = 0 ; i < jsonArray.length() ; i++) {
JSONObject object = (JSONObject) jsonArray.get(i);
unames[i] = object.getString("uname");
}
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.menu_list_row, unames, new int[] { R.id.LR_Name });
list.setAdapter(adapter);
}
}
}catch (Exception e) {
e.printStackTrace();
}

要将结果添加到 ListView,请点击此链接 http://www.mkyong.com/android/android-listview-example/传递 String[] unames

关于php - android如何获取json数组到listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11466188/

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