public class MainActivity extends AppCompatActivity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); asyncLoadData("http://japi.juhe.cn/health_knowledge/infoList?key=f1f5de8afb705895c13c349b6f8e6513"); } /** *加载数据 * 异步任务实现 * 请求方式 : GET */ private void asyncLoadData(String path){ new AsyncTask<String,Void,String>(){ @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); if(s == null) { return; } //1.解析json时句句 Gson gson = new Gson(); HelthInfo helthInfo = gson.fromJson(s,HelthInfo.class); List<HelthInfo.ResultBean.DataBean> lists = helthInfo.getResult().getData(); //2.与适配器绑定 MyBaseAdapter adapter = new MyBaseAdapter(MainActivity.this,lists); //3.映射到ListView lv.setAdapter(adapter); } @Override protected String doInBackground(String... params) { try { String path = params[0]; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int code = connection.getResponseCode(); if(code == 200){ //得到服务的数据 InputStream is = connection.getInputStream(); String result = StreamTools.readFromNetWork(is); return result; } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(path); } }
public class MyBaseAdapter extends BaseAdapter { private List<HelthInfo.ResultBean.DataBean> list; private Context context; public MyBaseAdapter(Context context, List<HelthInfo.ResultBean.DataBean> list){ this.list = list; this.context = context; } @Override public int getCount() { return list != null ? list.size() : 0; } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null){ convertView = convertView.inflate(context,R.layout.item,null); } ImageView ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon); TextView tvContent = (TextView) convertView.findViewById(R.id.tvContent); //赋值 tvContent.setText(list.get(position).getTitle()); //加载图片 loadImage(list.get(position).getImg(),ivIcon); return convertView; } /** * 加载图片 * @param path * @param ivIcon */ private void loadImage(String path, final ImageView ivIcon){ new AsyncTask<String,Void,Bitmap>(){ @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if(bitmap != null){ ivIcon.setImageBitmap(bitmap); }else{ ivIcon.setImageResource(R.mipmap.ic_launcher); } } @Override protected Bitmap doInBackground(String... params) { try { String path = params[0]; URL url = new URL(path); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int code = connection.getResponseCode(); if(code == 200){ InputStream is = connection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); return bitmap; } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(path); } }