okhttp

compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okio:okio:1.5.0'

public class MainActivity extends AppCompatActivity {


    private XListView xlv;
    private int ZHI = 0x11;
    private List<Bean.ResultBean.DataBean> data;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xlv = (XListView) findViewById(R.id.xlv);
        get("http://v.juhe.cn/toutiao/index?type=shehui&key=6dde6de11cc04328c65963b2951dc8b5");
//        fF("http://116.211.167.106/api/live/aggregation?uid=133825214&interest=1");
    }


    class MyAdapter extends BaseAdapter {
        public List<Bean.ResultBean.DataBean> list;
        private DisplayImageOptions op;
        public MyAdapter(List<Bean.ResultBean.DataBean> list) {

            this.list = list;

             op=new DisplayImageOptions.Builder().cacheOnDisk(true).cacheInMemory(true).build();

        }

        @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) {
            view = View.inflate(MainActivity.this, R.layout.item, null);
            TextView tv = view.findViewById(R.id.tv);
            tv.setText(list.get(i).getTitle());
            ImageView img=view.findViewById(R.id.img);
            ImageLoader.getInstance().displayImage(list.get(i).getThumbnail_pic_s(),img,op);
            return view;
        }
    }


    public void get(final String url1) {
        //子线程
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    final String uri = getURI(url1);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Gson gson = new Gson();
                            Bean bean = gson.fromJson(uri, Bean.class);
                           data = bean.getResult().getData();
                            MyAdapter myAdapter = new MyAdapter(data);
                            xlv.setAdapter(myAdapter);

                        }
                    });


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private String getURI(String url) throws IOException {
//ok的get的方法
        OkHttpClient client = new OkHttpClient();
        Request build = new Request.Builder()
                .url(url)
                .build();

        Response execute = client.newCall(build).execute();
        if (execute.isSuccessful()) {
            return execute.body().string();
        } else {
            return "";
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值