2013-07-29 32 views
-2

如何在imageview和textview中傳遞JSON值?如何在TextView和ImageView中顯示JSON值

我的代碼如下包含三個ImageView(img11,img2,img3)和TextView(txt1,txt2,txt3)。我想在這些視圖中顯示我的JSONArray,其中包含名稱和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> 
+0

在哪裏execly是你的問題?難道你不能解碼InputStream或其他?請指定您的例外信息。 –

+0

我的網絡連接是json parsng的完美olyphblem。如何解析這個json文件?哪個節點是數組?如何使JSON對象中的TextView的ImageView顯示「網址」名稱」 – user2619496

回答

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(); 
     } 
+0

它將打印所有三個網址和名稱在三個不同勢的TextView和ImageView的??? – user2619496

+0

我想告訴我的最後的畫面是這樣的http:// imgur。 com/Qv1qjjN – user2619496

+0

of cource not。現在你應該下載所有3個url的url數據,並將它解碼爲一個位圖,並且它們在ImageView中顯示它。 –