public class MainActivity extends Activity { private ListView listview; private ProgressDialog dialog; private String url = "http://m2.qiushibaike.com/article/list/suggest?page=1"; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 0: dialog.show(); break; case 1: List<Map<String, Object>> list=(List<Map<String, Object>>) msg.obj; listview.setAdapter(new MyBaseAdapter(list)); break; case 2: dialog.dismiss(); break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) this.findViewById(R.id.newslistView); dialog = new ProgressDialog(this); dialog.setIcon(R.drawable.ic_launcher); dialog.setTitle("提示: "); dialog.setMessage("正在下載!..o..!"); new Thread(new Runnable() { @Override public void run() { handler.sendEmptyMessage(0);// 顯示對話框 String json = loadUrlJson(url); List<Map<String, Object>> list = parseJOSN(json); Message message = Message.obtain(); message.what = 1; message.obj = list; handler.sendMessage(message); handler.sendEmptyMessage(2);// 關閉對話框 } }).start(); } // 下載字符串 public String loadUrlJson(String url) { try { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } // JSON解析 public List<Map<String, Object>> parseJOSN(String json) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); try { JSONObject jsonObject = new JSONObject(json); JSONArray jsonarray = jsonObject.getJSONArray("items"); for (int i = 0; i < jsonarray.length(); i++) { Map<String, Object> itemMap = new HashMap<String, Object>(); JSONObject jsonItems = jsonarray.getJSONObject(i); itemMap.put("image", jsonItems.getString("image")); itemMap.put("content", jsonItems.getString("content")); itemMap.put("comments_count", jsonItems.getString("comments_count")); JSONObject loginObj = jsonItems.getJSONObject("user"); if(loginObj!=null){ itemMap.put("login", loginObj.getString("login")); }else{ itemMap.put("login", ""); } list.add(itemMap); } } catch (JSONException e) { e.printStackTrace(); } return list; } class MyBaseAdapter extends BaseAdapter { private List<Map<String, Object>> list; public MyBaseAdapter(List<Map<String, Object>> list) { this.list = list; } @Override public int getCount() { return this.list.size(); } @Override public Object getItem(int position) { return this.list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = getLayoutInflater().inflate( R.layout.item_listview_main, null); ImageView imageView_cover = (ImageView) convertView .findViewById(R.id.imageView_cover); TextView text_login = (TextView) convertView .findViewById(R.id.text_login); TextView text_content = (TextView) convertView .findViewById(R.id.text_content); TextView text_commentscount = (TextView) convertView .findViewById(R.id.text_commentscount); holder.imageView_cover = imageView_cover; holder.text_login = text_login; holder.text_content = text_content; holder.text_commentscount = text_commentscount; convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.imageView_cover.setImageResource(R.drawable.ic_launcher); holder.text_login.setText(this.list.get(position).get("login") .toString()); holder.text_content.setText(this.list.get(position).get("content") .toString()); holder.text_commentscount.setText(this.list.get(position) .get("comments_count").toString()); return convertView; } class ViewHolder { private ImageView imageView_cover; private TextView text_login; private TextView text_content; private TextView text_commentscount; } } }