簡體   English   中英

將json數據拉到ListView

[英]pulling json data to ListView

我已經使用httpClient將json數據拉到ListView。 我能夠解析json數據,但不知道如何在我嘗試解析的ListView上顯示這些記錄

HttpGet httpget = new HttpGet("http://10.0.2.2:8000/api/ecp/merchantinfo/?format=json");
      try {
          HttpEntity entity;

          HttpResponse response;
          response = httpclient.execute(httpget);
          entity = response.getEntity();
          InputStream is = entity.getContent();
          BufferedReader reader = new BufferedReader(new InputStreamReader(is));
          StringBuilder sb = new StringBuilder();

          String line = null;
          try {
              while ((line = reader.readLine()) != null) {
                  sb.append((line + "\n"));
              }
             Toast.makeText(Landing.this, sb.toString(), Toast.LENGTH_SHORT).show();

          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              try {
                  is.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }

Xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow>

        <TextView
            android:id="@+id/tv_Land"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>
    <TableRow>
    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>

    </TableRow>


</TableLayout>

現在我需要在列表視圖中拉名稱字段json如下

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"categories": [{"api_key": "635c1a2fe3924626ddc38cc365a084d62e0201aa", "id": 1, "is_active": true, "name": "test1", "resource_uri": "/api/ecp/merchantcategory/1/"}], "contact": [{"ContactUsAddressCity": "kathmandu", "ContactUsAddressFax": "8989898", "ContactUsAddressLine1": "anamnagar32", "ContactUsAddressLine2": "kathmandu", "ContactUsAddressState": "AR", "ContactUsAddressZip": "78787", "ContactUsBusinessHours": "tetsgsasjajash nsakjaskjas nsajnjashashjsa", "ContactUsEmail": "timus2001@gmail.com", "ContactUsPhone": "78898998", "api_key": "635c1a2fe3924626ddc38cc365a084d62e0201aa", "id": 1, "resource_uri": "/api/ecp/merchantcontact/1/"}], "create_time": "2012-09-04T15:54:34", "current_state": 1, "graphics": [{"api_key": "635c1a2fe3924626ddc38cc365a084d62e0201aa", "bg": "/media/000001_background.png", "bgcolor": "#D92727", "button": "/media/000001_top.png", "fontcolor": "#1440CF", "id": 1, "photo": "/media/000001_icon.png", "resource_uri": "/api/ecp/merchantgraphics/1/", "splash": "/media/000001_splash.png"}], "id": 1, "modified_time": "2012-09-04T15:54:34", "name": "Super restro and bar", "resource_uri": "/api/ecp/merchantinfo/1/", "utcStateCreated": null, "utcStateDisabled": null, "utcStateEnabled": null, "utcStateUnsubscribed": null}]}

獲取JSON字符串后,創建一個jason對象。

JSONObject object = new JSONObject(sb.trim());
JSONArray objects = object.getJSONArray("objects");

然后解析json數組...

檢查鏈接: http : //developer.android.com/reference/org/json/JSONObject.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM