异步获取数据ListView显示

new MyTask().execute();

//异步获取数据显示
class MyTask extends AsyncTask {
        @Override         protected String doInBackground(String... params) {             String result = "";             try {                 URL url = new URL(params[0]);                 HttpURLConnection connection = (HttpURLConnection) url.openConnection();                 connection.setRequestMethod("GET");                 connection.setConnectTimeout(5000);                 connection.setReadTimeout(5000);                 if(connection.getResponseCode()==200){                     InputStream inputStream=connection.getInputStream();                     result=StreamToString.streamToStr(inputStream,"utf-8");                 }             } catch (Exception e) {                 e.printStackTrace();             }             return result;         }
        @Override         protected void onPostExecute(String s) {             //1.gson解析             Gson gson = new Gson();             Result result=gson.fromJson(s,Result.class);             //从对象中获取要显示的集合数据             list = result.getResults();
            //3.设置适配器             setAdapter();         }     }
    /**      * 设备适配器      */     private void setAdapter() {         if (adapter == null) {             adapter = new MyAdapter(this, list);             listView.setAdapter(adapter);         } else {             adapter.notifyDataSetChanged();         }     }

你可能感兴趣的:(异步获取数据ListView显示)