Android資料載入和Json解析——藍本

來源:互聯網
上載者:User

標籤:

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<String, Void, String>    {           @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<MyData> dataList = new ArrayList<MainActivity.MyData>();//聲明全域變數dataList FileAsyncTask fileAsyncTask = new FileAsyncTask();        fileAsyncTask.execute("http://192.168.56.1:8080/service.txt");

著作權聲明:本文部落格原創文章,部落格,未經同意,不得轉載。

Android資料載入和Json解析——藍本

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.