首先封装一个工具类 以方便在主方法调用它的方法
private static String tag="getJson"; public static String getJson(String urls){ try { //url对象封装字符串接口 URL u=new URL(urls); //用url打开连接,返回值用httpurlconnection HttpURLConnection urlc = (HttpURLConnection) u.openConnection(); //设置连接超时时间 urlc.setConnectTimeout(8000); //获取状态码 int responseCode = urlc.getResponseCode(); //判断状态码成功 if(responseCode==200){ //将字节流转换成字符流 InputStream is = urlc.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); //可拼接的字符串 StringBuilder sb=new StringBuilder(); String line=null; while((line=br.readLine())!=null){ sb.append(line); } String s = sb.toString(); return s; }else{ } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } //获取网络图片 public static Bitmap getNetBit(String urls){ try { //url对象封装图片 URL u=new URL(urls); //url连接,返回值用httpurlconnection HttpURLConnection urlConnection = (HttpURLConnection) u.openConnection(); //设置连接超时时间 urlConnection.setConnectTimeout(8000); //获取状态码 int in = urlConnection.getResponseCode(); //判断状态码 if(in==200){ InputStream is = urlConnection.getInputStream(); //BitmaoFactory是Android提供好的加工图片的工具类 Bitmap bitmap = BitmapFactory.decodeStream(is); return bitmap; } } catch (IOException e) { e.printStackTrace(); } return null; }
在主方法中要用到AsyncTask重写的两个方法
public String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1"; String urlBitmap = "https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg"; private ListView lv; ArrayList<Product.DataBean> dataBeanList=new ArrayList(); private My my; /** * Wuhuiying * @param savedInstanceState */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件 lv = (ListView) findViewById(R.id.lv); //设置适配器 //在主线程 new MAsyncTask().execute(urlString); my = new My(); lv.setAdapter(my); } /** * String:第一个传过来的参数 * void:进度 * string:接口请求成功返回的(json,bitmap) */ class MAsyncTask extends AsyncTask<String,Void,String>{ /** * 这个方法实现子线程里进行联网操作 * @param strings * @return */ @Override protected String doInBackground(String... strings) { String urlTemp=strings[0]; //直接用工具类获取json String netjson = Netutil.getJson(urlTemp); //返回给前台方法,主线程方法onpostExcute return netjson; } /** * 这个方法在主线程,更新 * @param s */ @Override protected void onPostExecute(String s) { super.onPostExecute(s); Log.e("why","onPostExecutr:"+s); //解析s,直接给控件展示 Gson gson = new Gson(); Product product = gson.fromJson(s, Product.class); //把一个集合的数据放入另一个数据中 List<Product.DataBean> temp=product.getData(); dataBeanList.addAll(temp); my.notifyDataSetChanged(); } } class My extends BaseAdapter{ private TextView tv; private ImageView iv; @Override public int getCount() { return dataBeanList.size(); } @Override public Object getItem(int i) { return dataBeanList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View v = View.inflate(MainActivity.this, R.layout.listview, null); tv = v.findViewById(R.id.tv); iv = v.findViewById(R.id.iv); tv.setText(dataBeanList.get(i).getNews_summary()); new MBitMapAsyncTask(iv).execute(urlBitmap); return v; } } class MBitMapAsyncTask extends AsyncTask<String,Void,Bitmap>{ private ImageView iv; public MBitMapAsyncTask(ImageView iv) { this.iv = iv; } @Override protected Bitmap doInBackground(String... strings) { return Netutil.getNetBit(strings[0]); } @Override protected void onPostExecute(Bitmap bitmap) { iv.setImageBitmap(bitmap); super.onPostExecute(bitmap); } }
最后别忘了在清单文件manifests中配置网络权限