How to pass JSON value in imageview and textview?
My code below contains three ImageView's (img11,img2,img3
) and TextView's (txt1,txt2,txt3
). I want to show my JSONArray, which contains name and url, in those views. In my code there is no ListView. How do I show my JSON values in my TextView and ImageView? The http connection is OK. The problem is in parsing the JSON array.
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"
}
]
}
Code:
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>