Android_XListView请求网络数据

 
  
/**
 * Created by 壹颗大金星 on 2017/9/7.
 */
public class Fragment2 extends Fragment implements XListView.IXListViewListener{

    Handler handler = new Handler();
    private View view;
    private XListView myxlistview;
    private List list = new ArrayList();
    private Myadapter myadapter;
    private List newslist;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment2, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        myxlistview = (XListView)view.findViewById(R.id.myxlistview);
        myxlistview.setPullLoadEnable(true);// 设置让它上拉,FALSE为不让上拉,便不加载更多数据

        new MyAsynctask().execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10");

        myxlistview.setXListViewListener(this);

    }

    class MyAsynctask extends AsyncTask {

        private StringBuilder builder;

        @Override
        protected String doInBackground(String... strings) {
            try {
                //获取url
                URL url = new URL(strings[0]);
                //请求网络
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                int code = urlConnection.getResponseCode();
                //判断是否返回成功
                if(code==200){
                    //获取网络信息
                    InputStream inputStream = urlConnection.getInputStream();
                    BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
                    builder = new StringBuilder();
                    String s = "";
                    //拼接
                    while((s=bf.readLine())!=null){
                        builder.append(s);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return builder.toString();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //Log.w("TAG", "onPostExecute: "+s );
            //解析
            Gson gson = new Gson();
            Bean bean = gson.fromJson(s, Bean.class);
            newslist = bean.getNewslist();
            //存入集合
            for (int i = 0; i< newslist.size(); i++){
                list.add(new List_bean(newslist.get(i).getCtime(), newslist.get(i).getTitle(), newslist.get(i).getPicUrl(), newslist.get(i).getUrl()));
            }
            myadapter = new Myadapter();
            myxlistview.setAdapter(myadapter);
            myxlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View view, int position, long id) {
                    String url = list.get(position).getUrl();
                    Intent intent = new Intent(getActivity(),Main2Activity.class);
                    intent.putExtra("url",url);
                    startActivity(intent);
                }
            });
        }
    }

    class Myadapter extends BaseAdapter {

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

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

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

        class ViewHowder{
            ImageView img;
            TextView tv1;
            TextView tv2;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            //获取数据
            List_bean list_bean = list.get(i);
            ViewHowder howder = new ViewHowder();
            if(view==null){
                //创建试图
                view = View.inflate(getActivity(),R.layout.list,null);
                howder.img = (ImageView)view.findViewById(R.id.imageView);
                howder.tv1 = (TextView) view.findViewById(R.id.textView);
                howder.tv2 = (TextView)view.findViewById(R.id.textView2);
                view.setTag(howder);
            }else{
                howder = (ViewHowder) view.getTag();
            }
            howder.tv1.setText(list_bean.getTitle());
            howder.tv2.setText(list_bean.getCtime());
            ImageLoader.getInstance().displayImage(list_bean.getPicUrl(),howder.img);
            return view;
        }
    }


    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //清空集合
                list.clear();
                //重新请求数据
                new MyAsynctask().execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10");
                completeRefresh();
            }
        },2000);

    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //添加数据
                for (int i = 0; i< newslist.size(); i++){
                    list.add(new List_bean(newslist.get(i).getCtime(), newslist.get(i).getTitle(), newslist.get(i).getPicUrl(), newslist.get(i).getUrl()));
                }
                //刷新输配器
                myadapter.notifyDataSetChanged();
                completeRefresh();
            }
        },2000);
    }

    public void completeRefresh(){
        myxlistview.stopRefresh();
        myxlistview.stopLoadMore();
        myxlistview.setRefreshTime("刚刚");
    }

}

你可能感兴趣的:(Android_XListView请求网络数据)