package com.example.nm.myapplication; import android.graphics.Bitmap; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.List; public class MainActivity extends AppCompatActivity { ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView) findViewById(R.id.list); // 实现自己写的类 asyn asyn = new asyn(); // 把地址放到execute里执行 asyn.execute("http://www.93.gov.cn/93app/data.do?channelId=0&startNum=0"); } // 第一步自己写一个类继承AsyncTask class asyn extends AsyncTask<String,Void,String>{ @Override protected String doInBackground(String... params) { StringBuilder builder=null; try { // 这是主方法里传回来的地址 URL url = new URL(params[0]); HttpURLConnection con = (HttpURLConnection)url.openConnection(); con.setRequestMethod("GET"); con.setReadTimeout(5000); con.setConnectTimeout(5000); if(con.getResponseCode()==200){ InputStream stream = con.getInputStream(); InputStreamReader reader = new InputStreamReader(stream); BufferedReader buf = new BufferedReader(reader); String s=null; builder = new StringBuilder(); while((s=buf.readLine())!=null){ builder.append(s); } } } catch (Exception e) { e.printStackTrace(); } // 手动返回值 return builder.toString(); } @Override // 这是手动重写出来的方法用来写解析和适配器的 protected void onPostExecute(String s) { super.onPostExecute(s); Gson gosn = new Gson(); // 这里是上面定义过来的s bena bena = gosn.fromJson(s, bena.class); List<com.example.nm.myapplication.bena.DataBean> list = bena.getData(); listView.setAdapter(new Adapter(MainActivity.this,list)); } } }