1. 程式人生 > >用AsyncTask和HttpURLConnection寫ListView

用AsyncTask和HttpURLConnection寫ListView

首先建專案建佈局檔案

<uses-permission android:name="android.permission.INTERNET"/>

設定網路匯入的資原始檔

public String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";

網路許可權

在Main方法的OnCreateView中找到listView控制元件並設定適配寫入方法用asyncTask引入資源

new MMasyncTask().execute(urlString);
class MMasyncTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... strings) {
            String netjson = NetUtil_11.getNetBitmap(strings[0]);
            return netjson;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();
            Product product = gson.fromJson(s, Product.class);
            //將原來的資料放到另一個新的集合當中去
            List<Product.DataBean> data = product.getData();
            dataBeanArrayList.addAll(data);
            //介面卡重新整理
            mAdapter.notifyDataSetChanged();
        }
    }

設定listView介面卡

public static String getNetBitmap(String string) {
        try {
            URL url = new URL(string);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            //設定連結超時的時間
            //urlConnection.setConnectTimeout(8000);
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200){
                InputStream inputStream = urlConnection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

                StringBuilder builder = new StringBuilder();
                String temp = "";
                while ((temp = reader.readLine()) != null){
                    builder.append(temp);
                }
                String string1 = builder.toString();
                return string1;
            }else{
                Log.e(tag,""+responseCode);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回一個空的字串
        return "";
    }

用一鍵生成建立Bean類

private class MAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return dataBeanArrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return dataBeanArrayList.get(position);
        }-


        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(getActivity(),R.layout.item_11,null);
            TextView textView = view.findViewById(R.id.tv_11);
            ImageView imageView = view.findViewById(R.id.iv_11);
            textView.setText(dataBeanArrayList.get(position).getNews_summary());

            new AsyncBitmapTask(imageView).execute(dataBeanArrayList.get(position).getPic_url());
            return view;
        }
    }

設定圖片的AsyncTask

private class AsyncBitmapTask extends AsyncTask<String, Void, Bitmap> {

        ImageView imageView;

        public AsyncBitmapTask(ImageView imageView) {
            this.imageView = imageView;
        }

        @Override
        protected Bitmap doInBackground(String... strings) {


            return NetUtil_11.getJson(strings[0]);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            imageView.setImageBitmap(bitmap);
        }
    }

根據個人喜好設定請求的東西

public static Bitmap getJson(String string) {
        try {
            URL url = new URL(string);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(8000);
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == 200){
                InputStream inputStream = urlConnection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }