如何在TextView和ImageView中显示JSON值

时间:2013-07-29 18:10:23

标签: android

如何在imageview和textview中传递JSON值?

我的代码包含三个ImageView(img11,img2,img3)和TextView(txt1,txt2,txt3)。我想在这些视图中显示我的JSONArray,其中包含name和url。在我的代码中没有ListView。如何在TextView和ImageView中显示我的JSON值? http连接正常。问题在于解析JSON数组。

JSON:

  ///////////////   this is my json file////////////
  {
   "worldpopulation": 
   [
      {
        "rank":1,
        "name": "BREAKFAST",
        "url": "http://niel986.files.wordpress.com/2012/07/fast-food.jpg"
      },
      {   
        "rank":2,
        "name": "LUNCH ",
        "url": "http://www.bubblews.com/assets/images/news/1107772406_1370520219.gif"
      },
      {  
        "rank":3,
        "name": "SUPPER",
        "url": "http://2.bp.blogspot.com/_JU_j7jj5TjU/TSBQKRukf1I/AAAAAAAAAs8/X1w5_z6pjwQ
/s1600/chicken-biryani.jpg"
      }
    ]
 }

代码:

  ImageView img1 = (ImageView) findViewById(R.id.img1);
  TextView txt1 = (TextView) findViewById(R.id.txt1);

  ImageView img2 = (ImageView) findViewById(R.id.img2);
  TextView txt2 = (TextView) findViewById(R.id.txt2);

  ImageView img3 = (ImageView) findViewById(R.id.img3);
  TextView txt3 = (TextView) findViewById(R.id.txt3);

  MenuSelect = Utils.MenuSelect;

  try {
      HttpClient client = new DefaultHttpClient();
      HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000);
      HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
      HttpUriRequest request = new HttpGet(MenuSelect);
      HttpResponse response = client.execute(request);
      InputStream atomInputStream = response.getEntity().getContent();
      BufferedReader in = new BufferedReader(new InputStreamReader(atomInputStream));

      String line;
      String str = "";
      while ((line = in.readLine()) != null) {
          str += line;
      }

      JSONObject json = new JSONObject(str);
      JSONArray data = json.getJSONArray("worldpopulation");

      String Name1 = data.getString("name");
      String url1 = person.getString("url");
      txt1.setText(Name1);
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                url1).getContent());
      img1.setImageBitmap(bitmap);

  } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (IOException e) {
      // TODO Auto-generated catch block
      // IOConnect = 1;
      e.printStackTrace();
  } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }

XML

<LinearLayout 

 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_gravity="center"
 android:gravity="center"
 android:orientation="horizontal" >

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
         android:id="@+id/img1"
        android:layout_width="70dp"
        android:layout_height="80dp"
        android:contentDescription="@null"
        android:background="@drawable/imagebgborder"
        android:src="@drawable/ic_launcher" />

    <TextView
         android:id="@+id/txt1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:textStyle="bold"
        android:contentDescription="@null"
        android:text="@string/hello_world" />
  </LinearLayout>

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="vertical" >

    <ImageView
         android:id="@+id/img2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@null"
        android:src="@drawable/ic_launcher" />

    <TextView
         android:id="@+id/txt2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:contentDescription="@null"
        android:text="@string/hello_world" />
  </LinearLayout>

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
         android:id="@+id/img3"

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@null"
        android:src="@drawable/ic_launcher" />

    <TextView
         android:id="@+id/txt3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp"
        android:contentDescription="@null"
        android:text="@string/hello_world" />
 </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

我认为你需要这个

try {
            JSONObject json = new JSONObject(str);
            JSONArray data = json.getJSONArray("worldpopulation");
            JSONObject jsonOrder = (JSONObject)data.get(0);
            txt1.setText (jsonOrder.getString("name"));
            img1.setImageBitmap( /* decoded url input stream*/);

            JSONObject jsonOrder = (JSONObject)data.get(1);
            txt2.setText (jsonOrder.getString("name"));
            img2.setImageBitmap( /* decoded url input stream*/);

            JSONObject jsonOrder = (JSONObject)data.get(2);
            txt3.setText (jsonOrder.getString("name"));
            img3.setImageBitmap( /* decoded url input stream*/);

        } catch (JSONException e) {
            e.printStackTrace();
        }