我的项目是从服务器获取 json 数据并在运行代码时显示在列表视图中我的代码不起作用:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, this.datadrinks()));
}
public ArrayList<String> datadrinks()
{
ArrayList<String> listItems = new ArrayList<String>();
try {
URL twitter = new URL("http://10.0.2.2:50667/expression%20web4/GetAllDrinkItems.ashx");
URLConnection tc = twitter.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
JSONObject ja = new JSONObject(line);
JSONArray jobj=ja.getJSONArray("lstDrinkItems");
for (int i = 0; i < jobj.length(); i++) {
JSONObject jo = jobj.getJSONObject(i);
listItems.add(jo.getString("Name"));
}
}
}catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listItems;
}
}