AsyncTask从网络下载JSON并解析,在TextView中展示

1.自定义一个类实现系统AsyncTask

public class MyAsyncTask extends AsyncTask {
    private List> toList = new ArrayList>();
    private Context context;
    private TextView textView;
    private ProgressDialog dialog;
    public MyAsyncTask(Context context,TextView textView){
        this.context = context;
        this.textView = textView;
        dialog = new ProgressDialog(context);
        dialog.setIcon(R.drawable.ic_launcher);
        dialog.setTitle("友情提示");
        dialog.setMessage("正在为您努力加载中");
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.show();
    }
    @Override
    protected byte[] doInBackground(String... params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(params[0]);
        try {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            if(httpResponse.getStatusLine().getStatusCode() == 200){
                HttpEntity entity = httpResponse.getEntity();
                byte [] data = EntityUtils.toByteArray(entity);
                return data;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    @Override
    protected void onPostExecute(byte[] result) {
        super.onPostExecute(result);
        if(result != null){
            //将下载下来的字节数组转换为字符串
            String jsonString = new String(result);
            //
            toList = ParseBytetoJSON.parseNewtoList(jsonString);
            //在TextView 里面展示解析后JSON数据
            textView.setText(toList.toString());
        }
        else{
            Toast.makeText(context, "下载JSON数据失败", Toast.LENGTH_LONG).show();
        }
        dialog.dismiss();
    }

}

2.解析JSON,返回list集合

/**
 * 解析JSON数据
 * @author Administrator
 *
 */
public class ParseBytetoJSON {
    private static List> list = new ArrayList>();
    public static List> parseNewtoList(String jsonString){
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            if(jsonObject.getString("status").equals("ok")){
                JSONObject jsonObject_paramz = jsonObject.getJSONObject("paramz");
                JSONArray jsonArray_feeds = jsonObject_paramz.getJSONArray("feeds");
                for(int i=0 ;i map = new HashMap();
                    map.put("subject", jsonObject_data.getString("subject"));
                    map.put("summary", jsonObject_data.getString("summary"));
                    map.put("cover", jsonObject_data.getString("cover"));
                    map.put("changed", jsonObject_data.getString("changed"));
                    
                    list.add(map);
                }
            }
            
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}

3.测试Activity

public class MainActivity extends Activity {
    private MyAsyncTask myAsyncTask;
    private TextView textView;
    private String url = "http://litchiapi.jstv.com/api/GetFeeds?column=0&PageSize=10&pageIndex=1";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView = (TextView)findViewById(R.id.tv_textView);
        myAsyncTask = new MyAsyncTask(this, textView);
        myAsyncTask.execute(url);
    }
}

你可能感兴趣的:(AsyncTask从网络下载JSON并解析,在TextView中展示)