装载的ListView在Android的使用AsyncTask的JSON数据 [英] Loading ListView with JSON data using AsyncTask in Android

查看:121
本文介绍了装载的ListView在Android的使用AsyncTask的JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个网址 http://inspirontrance.com/tpb.json 解析JSON数据。我的code加载一定次数后作品,未经AsyncTask的,但启用了AsyncTask的,应用程序崩溃。虽然我的codeS似乎确定,但不知该应用程序在异步过程冻结。

下面是我的code,

 公共类AndroidJSONParsingActivity扩展ListActivity {    // URL使请求
    私有静态字符串URL =htt​​p://inspirontrance.com/tpb.json;    // JSON节点名称
    私有静态最后弦乐TAG_UPLOADS =上传;
    私有静态最后弦乐TAG_ID =ID;
    私有静态最后弦乐TAG_NAME =名;
    私有静态最后弦乐TAG_DATE =日期;    ProgressDialog pDialog;    // JSONArray
    JSONArray上传= NULL;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        新MyAsyncTask()执行(URL);
        //发送HTTP请求之前,显示进度对话框
        pDialog =新ProgressDialog(本);
        pDialog.setMessage(请稍候...);
        pDialog.setIndeterminate(真);
        pDialog.setCancelable(假);
        pDialog.show();
    }    类MyAsyncTask扩展
            AsyncTask的<字符串,整数的ArrayList<&HashMap的LT;字符串,字符串>>> {        //为哈希映射的ListView
        ArrayList的<&HashMap的LT;字符串,字符串>> UploadsList =新的ArrayList<&HashMap的LT;字符串,字符串>>();        @覆盖
        保护的ArrayList<&HashMap的LT;字符串,字符串>> doInBackground(
                串... PARAMS){            //创建JSON解析器实例
            JSONParser jParser =新JSONParser();            // URL从获取JSON字符串
            JSONObject的JSON = jParser.getJSONFromUrl(URL);            尝试{
                //获取上传的数组
                上传= json.getJSONArray(TAG_UPLOADS);                //通过所有上传循环
                的for(int i = 0; I< uploads.length();我++){
                    JSONObject的C = uploads.getJSONObject(I)                    //存储在变量中的每个JSON项目
                    字符串ID = c.getString(TAG_ID);
                    字符串名称= c.getString(TAG_NAME);
                    字符串日期= c.getString(TAG_DATE);                    //创建新的HashMap
                    HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                    //将每个子节点的HashMap键=>值
                    map.put(TAG_ID,身份证);
                    map.put(TAG_NAME,名);
                    map.put(TAG_DATE,日期);                    //添加HashList到ArrayList的
                    UploadsList.add(地图);
                }
            }赶上(JSONException E){
                e.printStackTrace();
            }            返回UploadsList;
        }    }    保护无效onPostExecute(ArrayList的<&HashMap的LT;字符串,字符串>>的结果){        pDialog.dismiss();        ListAdapter适配器=新SimpleAdapter(这一点,因此,
                R.layout.list_item,
                新的String [] {TAG_NAME,TAG_ID,TAG_DATE},新的INT [] {
                        R.id.name,R.id.id,R.id.date});        setListAdapter(适配器);
    }
}


解决方案

下面就是我会做。这是很好的进入你的任务中做你的pre /岗位工作的习惯。

 类MyAsyncTask扩展
        AsyncTask的<字符串,整数的ArrayList<&HashMap的LT;字符串,字符串>>> {    //为哈希映射的ListView
    ArrayList的<&HashMap的LT;字符串,字符串>> UploadsList =新的ArrayList<&HashMap的LT;字符串,字符串>>();    @覆盖
    在preExecute保护无效(){
        //发送HTTP请求之前,显示进度对话框
        pDialog =新ProgressDialog(本);
        pDialog.setMessage(请稍候...);
        pDialog.setIndeterminate(真);
        pDialog.setCancelable(假);
        pDialog.show(); }    @覆盖
    保护的ArrayList<&HashMap的LT;字符串,字符串>> doInBackground(
            串... PARAMS){        //创建JSON解析器实例
        JSONParser jParser =新JSONParser();        // URL从获取JSON字符串
        JSONObject的JSON = jParser.getJSONFromUrl(URL);        尝试{
            //获取上传的数组
            上传= json.getJSONArray(TAG_UPLOADS);            //通过所有上传循环
            的for(int i = 0; I< uploads.length();我++){
                JSONObject的C = uploads.getJSONObject(I)                //存储在变量中的每个JSON项目
                字符串ID = c.getString(TAG_ID);
                字符串名称= c.getString(TAG_NAME);
                字符串日期= c.getString(TAG_DATE);                //创建新的HashMap
                HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();                //将每个子节点的HashMap键=>值
                map.put(TAG_ID,身份证);
                map.put(TAG_NAME,名);
                map.put(TAG_DATE,日期);                //添加HashList到ArrayList的
                UploadsList.add(地图);
            }
        }赶上(JSONException E){
            e.printStackTrace();
        }        返回UploadsList;
    }    @覆盖
    保护无效onPostExecute(ArrayList的<&HashMap的LT;字符串,字符串>>的结果){        pDialog.dismiss();        ListAdapter适配器=新SimpleAdapter(这一点,因此,
                R.layout.list_item,
                新的String [] {TAG_NAME,TAG_ID,TAG_DATE},新的INT [] {
                R.id.name,R.id.id,R.id.date});        setListAdapter(适配器);
    }
}

I want to parse JSON data from this url http://inspirontrance.com/tpb.json. My code works without the AsyncTask but with the AsyncTask enabled, app crashes after loading for a certain number of times. Although my codes seems ok but somehow the app freezes during the async process.

Here is my code,

    public class AndroidJSONParsingActivity extends ListActivity {

    // url to make request
    private static String url = "http://inspirontrance.com/tpb.json";

    // JSON Node names
    private static final String TAG_UPLOADS = "uploads";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_DATE = "date";

    ProgressDialog pDialog;

    // JSONArray
    JSONArray uploads = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        new MyAsyncTask().execute(url);
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    class MyAsyncTask extends
            AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {

        // Hashmap for ListView
        ArrayList<HashMap<String, String>> UploadsList = new ArrayList<HashMap<String, String>>();

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(
                String... params) {

            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(url);

            try {
                // Getting Array of Uploads
                uploads = json.getJSONArray(TAG_UPLOADS);

                // looping through All Uploads
                for (int i = 0; i < uploads.length(); i++) {
                    JSONObject c = uploads.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String date = c.getString(TAG_DATE);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_DATE, date);

                    // adding HashList to ArrayList
                    UploadsList.add(map);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return UploadsList;
        }

    }

    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

        pDialog.dismiss();

        ListAdapter adapter = new SimpleAdapter(this, result,
                R.layout.list_item,
                new String[] { TAG_NAME, TAG_ID, TAG_DATE }, new int[] {
                        R.id.name, R.id.id, R.id.date });

        setListAdapter(adapter);
    }
}

解决方案

Here's how I'd do it. It's good to get into the habit of doing your pre/post work within your task.

class MyAsyncTask extends
        AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> UploadsList = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();        }

    @Override
    protected ArrayList<HashMap<String, String>> doInBackground(
            String... params) {

        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Uploads
            uploads = json.getJSONArray(TAG_UPLOADS);

            // looping through All Uploads
            for (int i = 0; i < uploads.length(); i++) {
                JSONObject c = uploads.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String date = c.getString(TAG_DATE);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_DATE, date);

                // adding HashList to ArrayList
                UploadsList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return UploadsList;
    }

    @Override
    protected void onPostExecute(ArrayList<HashMap<String, String>> result) {

        pDialog.dismiss();

        ListAdapter adapter = new SimpleAdapter(this, result,
                R.layout.list_item,
                new String[] { TAG_NAME, TAG_ID, TAG_DATE }, new int[] {
                R.id.name, R.id.id, R.id.date });

        setListAdapter(adapter);
    }
}

这篇关于装载的ListView在Android的使用AsyncTask的JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆