AsyncTask异步加载和Gson

public class MainActivity extends Activity {
    private List<Goods> goods=new ArrayList<Goods>();

    private ListView lv;
    private SmartImageView smartImageView;

    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        adapter = new MyAdapter(goods, this);
        lv.setAdapter(adapter);
        //获得数据
        huodeshuju();
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Intent intent=new Intent(MainActivity.this,TwoActivity.class);
                startActivity(intent);
                
                
            }
        });
    }
    //获得数据
    private void huodeshuju() {
        //使用异步
        new AsyncTask<String, Void, String>(){

            @Override
            protected String doInBackground(String... params) {
                
                /*try {
                    URL url=new URL(params[0]);
                    HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
                    urlConnection.setConnectTimeout(5000);
                    urlConnection.setReadTimeout(5000);
                    urlConnection.setRequestMethod("GET");
                    urlConnection.connect();
                    int code=urlConnection.getResponseCode();
                    if (code==200) {
                        InputStream inputStream=urlConnection.getInputStream();
                        BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
                        String readerline;
                        StringBuffer buffer=new StringBuffer();
                        while ((readerline=reader.readLine())!=null) {
                            buffer.append(readerline);
                            
                        }
                        String str=buffer.toString();
                        return str;
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }*/
                
                
                
                
                try {
                    HttpClient httpClient=new DefaultHttpClient();
                    HttpGet httpGet=new HttpGet(params[0]);
                    HttpResponse response = httpClient.execute(httpGet);
                    StatusLine statusLine=response.getStatusLine();
                    int code=statusLine.getStatusCode();
                    if (code==200) {
                        HttpEntity httpEntity=response.getEntity();
                        String str=EntityUtils.toString(httpEntity, "utf-8");
                        return str.toString();
                    }
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                
                return null;
            }
            protected void onPostExecute(String result) {
                try {
                    JSONObject obj=new JSONObject(result);
                    JSONObject list=obj.getJSONObject("list");
                    JSONArray articles=list.getJSONArray("articles");
                    Type type=new TypeToken<List<Goods>>(){
                        
                    }.getType();
                    
                    Gson gson=new Gson();
                    List<Goods> goodss=gson.fromJson(articles.toString(), type);
                    for (Goods goods2 : goodss) {
                        
                        String title=goods2.getTitle();
                        String litpic=goods2.getLitpic();
                        String typeid=goods2.getTypeid();
                        Goods goods3=new Goods(title, litpic, typeid);
                        
                        goods.add(goods3);
                        
                        
                    }
                    //Log.i("++++++++", goods.toString());
                    //=======================
                    //必须刷新
                    adapter.notifyDataSetChanged();
                    //======================
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            };
            
        }.execute("http://101.200.142.201:8080/tqyb/newsList.json");
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

原文地址:https://www.cnblogs.com/changyiqiang/p/5732761.html