Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android數據加載及Json解析——原始版

Android數據加載及Json解析——原始版

編輯:關於Android編程

1、創建要下載數據的實體類

 class MyData
    {
        String imagepath;
        String title;
        String desc;

        public MyData(String imagepath, String title, String desc)
        {
            super();
            this.imagepath = imagepath;
            this.title = title;
            this.desc = desc;
        }

    }

2、AsyncTask數據加載及Json解析類

  class FileAsyncTask extends AsyncTask
    {   

        @Override
        protected String doInBackground(String... params)
        {
            InputStream is = null;
            ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(4000);
            try
            {   
                URL url = new URL(params[0]);
                URLConnection openConnection = url.openConnection();
                //設置連接超時的時間
                openConnection.setConnectTimeout(3000);
                //數據讀取超時的時間
                openConnection.setReadTimeout(3000);
                is = openConnection.getInputStream();
                byte[] buffer = new byte[1024];
                int len;
                while (-1 != (len = is.read(buffer)))
                {
                    byteArrayBuffer.append(buffer, 0, len);
                }

            }
            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {   
                // TODO Auto-generated catch block
                runOnUiThread(new Runnable()
                {
                    public void run()
                    {   
                        //設置加載異常時的控件,通常顯示進度條
                    }   
                });
                e.printStackTrace();
            }
            finally
            {
                if (is != null)
                {
                    try
                    {
                        is.close();
                    }
                    catch (IOException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            String res = new String(byteArrayBuffer.toByteArray(), 0,
                    byteArrayBuffer.length());
            return res;
        }

        // 解析jsonArray數據
        @Override
        protected void onPostExecute(String result)
        {
            try
            {
                JSONArray jsonArray = new JSONArray(result);
                for (int i = 0; i < jsonArray.length(); i++)
                {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String imagepath = jsonObject.getString("imagepath");
                    String title = jsonObject.getString("title");
                    String desc = jsonObject.getString("desc");
                    MyData myData = new MyData(imagepath, title, desc);
                    dataList.add(myData);//添加數據
                }
                myAdapter.notifyDataSetChanged();//通知適配器數據改變

            }
            catch (JSONException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            super.onPostExecute(result);
        }
    }

3、調用

 private ArrayList dataList = new ArrayList();//聲明全局變量dataList 

FileAsyncTask fileAsyncTask = new FileAsyncTask();
        fileAsyncTask.execute("http://192.168.56.1:8080/service.txt");
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved