美文网首页
AsyncTask从网络下载JSON并解析,在TextView中

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

作者: Mr_LJ | 来源:发表于2016-07-09 18:58 被阅读185次

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

    public class MyAsyncTask extends AsyncTask<String, Void, byte[]> {
        private List<Map<String,String>> toList = new ArrayList<Map<String,String>>();
        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<Map<String,String>> list = new ArrayList<Map<String,String>>();
        public static List<Map<String,String>> 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<jsonArray_feeds.length();i++){
                        JSONObject jsonObject_feeds = jsonArray_feeds.getJSONObject(i);
                        JSONObject jsonObject_data = jsonObject_feeds.getJSONObject("data");
                        
                        Map<String ,String > map = new HashMap<String ,String>();
                        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中

          本文链接:https://www.haomeiwen.com/subject/iwzrjttx.html