使用异部类get网络获取json

public class MainActivity extends AppCompatActivity {
    private ListView lv;
    private List list;
    private List urllist=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv= (ListView) findViewById(R.id.listview);

        getData();
    }
    public void getData(){
        new AsyncTask() {
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson=new Gson();
                MenuInfo menuInfo=gson.fromJson(s,MenuInfo.class);
                list=menuInfo.getData();
                lv.setAdapter(new MyAdapter());
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                        Intent it=new Intent(MainActivity.this,SecondActivity.class);
                        startActivity(it);
                    }
                });
            }

            @Override
            protected String doInBackground(String... strings) {
                try {
                    String path=strings[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==HttpURLConnection.HTTP_OK){
                        InputStream is=connection.getInputStream();
                        ByteArrayOutputStream baos=new ByteArrayOutputStream();
                        int len=0;
                        byte[] buffer=new byte[1024];
                        while ((len=is.read(buffer))!=-1){
                            baos.write(buffer,0,len);
                        }
                        return  baos.toString();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute("http://shouyoutoutiao.app.17wanba.com/toutiao/get_list");
    }
 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;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder=null;
            if(view==null){
                holder=new ViewHolder();
                view=View.inflate(MainActivity.this,R.layout.item01,null);

                holder.ivIcon=view.findViewById(R.id.ivIcon);
                holder.tvContent=view.findViewById(R.id.tvContent);
                holder.tvTitle=view.findViewById(R.id.tvTitle);
                view.setTag(holder);
            }else{
                holder= (ViewHolder) view.getTag();
            }
            holder.tvTitle.setText(list.get(i).getTitle());
            holder.tvContent.setText(list.get(i).getSource_name());
              获取图片
           ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(MainActivity.this).build();
            ImageLoader.getInstance().init(configuration);
            DisplayImageOptions options=new DisplayImageOptions.Builder()
                    .showImageOnFail(R.mipmap.ic_launcher)
                    .showImageOnLoading(R.mipmap.ic_launcher)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .displayer(new RoundedBitmapDisplayer(5))
                    .build();

            ImageLoader.getInstance().displayImage(list.get(i).getImg().get(0).getUrl(),holder.ivIcon,options);

            return view;
        }
    }
    class ViewHolder {
        TextView tvTitle;
        TextView tvContent;
        ImageView ivIcon;
    }
}

你可能感兴趣的:(使用异部类get网络获取json)