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 ""; } } }