hyljava

          利用AsyncHttpClient與服務(wù)器端傳輸數(shù)據(jù)

          創(chuàng)建一個顯示的界面xml
           <ListView
                  android:id="@+id/lv_show_view"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentTop="true"
                  android:layout_marginTop="16dp" >
              </ListView>

          再創(chuàng)建一個item.xml
          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="    android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
             
           <TextView
               android:layout_width="250dip"
               android:layout_height="wrap_content"
               android:id="@+id/title"
              
              
               />
           <TextView
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:id="@+id/timelength"
              
              
               />
          </LinearLayout>


          導(dǎo)入AsyncHttpClient需要的類

          之后


            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_show_list_activy);
            lv_show_view = (ListView) findViewById(R.id.lv_show_view);


             AsyncHttpClient client=new  AsyncHttpClient();
            
             String  url = "
               + "測試方法";
             client.get(url, new AsyncHttpResponseHandler() {
             
            
             public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
               List<Video> list=new ArrayList<Video>();
              try {
               Toast.makeText(ShowListActivy.this,statusCode+"", 1).show();
               
               String json = new String(responseBody);
                JSONArray array = new JSONArray(json);
               for(int i=0 ; i < array.length() ; i++){
                JSONObject item= array.getJSONObject(i);
               
                String id = item.getString("id");
                String title = item.getString("title");
                String timelength = item.getString("time");
                Log.e("jsonget", id+title+timelength);
                list.add(new Video( id, title, Integer.parseInt(timelength)));
               }
               List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
               for (Video v : list) {
                Map<String, Object> it = new HashMap<String, Object>();
                it.put("id", v.getId());
                it.put("title", v.getTitle());
                it.put("timelength", v.getTime());
                data.add(it);
               }
               SimpleAdapter adapter = new SimpleAdapter(ShowListActivy.this, data,R.layout.item, new String[] { "title", "timelength" },new int[] { R.id.title, R.id.timelength });
               lv_show_view.setAdapter(adapter);

               
               
               
              } catch ( Exception e) {
                Log.e("MainActivity", e.toString());
               
              }     
              
              
             }
             
            
             public void onFailure(int statusCode, Header[] headers,
               byte[] responseBody, Throwable error) {
              Toast.makeText(ShowListActivy.this,"shibai", 1).show();
              
             }
            });
            顯示出傳過來的json結(jié)果:
           
            
           

          本文章只是自己學(xué)習(xí)筆記,大家要慎重借鑒





          posted on 2013-12-03 14:30 何云隆 閱讀(318) 評論(0)  編輯  收藏 所屬分類: Android

          主站蜘蛛池模板: 巴彦淖尔市| 麦盖提县| 新余市| 子洲县| 崇仁县| 曲阜市| 海晏县| 江永县| 濮阳县| 金门县| 司法| 黎平县| 邵阳市| 温州市| 饶阳县| 扶风县| 云和县| 萨嘎县| 辽宁省| 宕昌县| 漳州市| 溆浦县| 赤水市| 灵石县| 睢宁县| 甘南县| 武夷山市| 博罗县| 鹤峰县| 成都市| 剑阁县| 惠来县| 涡阳县| 民县| 万载县| 溧水县| 手机| 静宁县| 偃师市| 鄂温| 合江县|