我想从服务器获取数据并将其保存在listview中我写了正确的代码没有错误但是当我点击活动app时停在这里是我的活动代码/
public class UserListActivity extends ActionBarActivity {
String myJSON;
private static final String TAG_RESULTS="result";
// private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL ="email";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_list);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String,String>>();
getData();
}
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
//String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
HashMap<String,String> users = new HashMap<String,String>();
// persons.put(TAG_ID,id);
users.put(TAG_NAME,name);
users.put(TAG_EMAIL,email);
personList.add(users);
}
ListAdapter adapter = new SimpleAdapter(
UserListActivity.this, personList, R.layout.list_item,
new String[]{TAG_NAME,TAG_EMAIL},
new int[]{R.id.name, R.id.email}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://samplechatapp.gear.host/Get-Data.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
}
这是log cat消息:
这是我想要的服务器上的数据。