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

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);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,341评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,120评论 19 139
  • 介绍自己负责的部分,如何实现的。 自定义view viewGroup activity的启动流程 事件传递及滑动冲...
    东经315度阅读 5,068评论 1 4
  • 想法~表达(影响想法跟表达的原因:语言和词汇,逻辑和结构,意愿和状态)~接受(沟通媒介、聆听能力、听者的状态和意愿...
    黎帅阅读 4,430评论 0 0
  • @一只不爱爬树的考拉 我问舍友,过两天就要考研了,很快就解放了,开心吗? 不开心。一脸愁容 舍友是进大学就抱定信念...
    一只不爱爬树的考拉阅读 3,991评论 0 7

友情链接更多精彩内容