备用的

//优化
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Holer holer;
    if (convertView == null){
        convertView = View.inflate(context,R.layout.item,null);
        holer = new Holer();
        holer.textView = convertView.findViewById(R.id.ftext1);
        holer.imageView = convertView.findViewById(R.id.iv1);

        convertView.setTag(holer);
    }else{
        holer = (Holer) convertView.getTag();
    }
    holer.textView.setText(list.get(position).getNews_summary());

    new MyBitMapAsyncTask(holer.imageView).execute(list.get(position).getPic_url());

    return convertView;
}

//Myapp要写在oncreate

ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(MyApp.this).build();
ImageLoader instance = ImageLoader.getInstance();
instance.init(build);

//创键new MyAsynTask().execute(urlString);

new MyAsynTask().execute(urlString);//
class NetworkUtils {
    public static String getJson(String string) {

        try {
            URL url = new URL(string);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();
            while (responseCode == 200){
                InputStream inputStream = urlConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder = new StringBuilder();
                String temp;

                while ((temp = bufferedReader.readLine())!=null){
                    stringBuilder.append(temp);
                }

                return stringBuilder.toString();

            }

        } catch (Exception e) {
            e.printStackTrace();
        }


        return "";
    }

//图片的

URL url = new URL(string);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
while (responseCode == 200){
    InputStream inputStream = urlConnection.getInputStream();
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
    return bitmap;
}

//AsyncTask创建

private class MyAsynTask extends AsyncTask<String ,Void , String> {
    @Override
    protected String doInBackground(String... strings) {
        String json = NetworkUtils.getJson(strings[0]);
        return json;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Gson gson = new Gson();
        Bean bean = gson.fromJson(s, Bean.class);

        ArrayList<Bean.DataBean> data = bean.getData();
        list.addAll(data);
        myAdapter.notifyDataSetChanged();

    }
}
private void GetGson() {
    new Thread(new Runnable() {
        @Override
        public void run() {

            String json = NetworkUtils.getJson(urlString);
            Message message = handler.obtainMessage();
            message.what = 1;
            message.obj =json;
            handler.sendMessage(message);

        }
    }).start();

}
@SuppressLint("HandlerLeak")
private Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what ==1){
            String json = (String) msg.obj;
            Gson gson = new Gson();
            Bean bean = gson.fromJson(json, Bean.class);
            ArrayList<Bean.DataBean> data = bean.getData();
            list.addAll(data);
            myAdapter2.notifyDataSetChanged();;
        }

    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43738523/article/details/85377402
今日推荐