Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.json.main;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.json.JSONTokener;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class JSONtestActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //TextView tv1 = (TextView)findViewById(R.id.TextView01);
- //tv.setText("Hey see this,its working..");
- try {
- /* Inflate TextView from the layout */
- TextView tv = (TextView)findViewById(R.id.TextView01);
- /* JSON data considered as an example. Generally this data is obtained
- from a web service.*/
- String json = "{"+
- " \"name\" :\"ashish\", " +
- " \"message\" :\"hey see this\", " +
- " \"place\" :\"gurgaon\", " +
- " \"date\" :\"aaj ka date hai \", " +
- " }";
- JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
- String name = object.getString("name");
- String message = object.getString("message");
- String place = object.getString("place");
- String date = object.getString("date");
- tv.setText("Name : " + name + "\n \n");
- tv.setText("Message" + message + "\n \n");
- tv.setText("Place" + place + "\n \n ");
- tv.setText("Data" + date + "\n \n");
- } catch (JSONException e) {
- e.printStackTrace();
- }
- catch(Exception ex){
- ex.printStackTrace();
- }
- }
- }
- /*
- here is XML >
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:id="@+id/TextView01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="No data"/>
- </LinearLayout>
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement