0

I am totally new to java/android . I have searched many tutorials over the internet. But those didnt helped me. So I have posted this problem here.

I have simple json data in php like below,

[{"title":"re3","notice":"etrf4w"},{"title":"vfdxg","notice":"fdgd"},  {"title":"notice1","notice":"details..."}]

I want to show this json data in android as a plain text like below,

Title:re3

notice: etrf4w ..................... ...................

How could I do that. please help me.

2
  • Have you tried anything? Commented Jan 22, 2014 at 9:07
  • Are you considering using libraries to carry out this json parsing? Commented Jan 22, 2014 at 9:09

4 Answers 4

2

Try this code

    JSONArray jsonArray = new JSONArray(responseData);
            ArrayList<String> list =new ArrayList<String>();        
            if(jsonArray != null){
                for(int i=0 ;i < jsonArray.length() ;i++){          
                list.add(jsonArray.getJSONObject(i).getString("title").toString());     
                            list.add(jsonArray.getJSONObject(i).getString("notice").toString());            
                }
            }
return list
Sign up to request clarification or add additional context in comments.

Comments

1

Here is the exmaple

private Handler handler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        switch(msg.what){
            case 1:{
                showList();
            }break;
        }
    }

};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    /*
    Your initialization code here
    */

    new Thread(){

        @Override
        public void run() {
            String resp = loadURLData(JSON_URL);

            MainActivity.this.items = new ArrayList<Item>();

            try{
                JSONArray items = new JSONArray(resp);

                for(int i = 0; i < items.length(); i++){
                    JSONObject item = items.getJSONObject(i);

                    String title = item.getString("title");
                    String notice = item.getString("notice");

                    Item it = new Item(title, notice);
                }

                handler.sendEmptyMessage(1);
            }catch(JSONException jSONEx){
            }
        }

    }.start();
}

private void showList(){
    ListAdapter adapter = new MyAdapter(this, items);
    listView.setAdapter(adapter);
}

private static String loadURLData(String msgsUrl){
    try{
        URL url = new URL(msgsUrl); 
        URLConnection conn = url.openConnection();
        InputStreamReader streamReader = new InputStreamReader(conn.getInputStream());

        BufferedReader br = new BufferedReader(streamReader);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while((line = br.readLine()) != null) {
            sb.append(line);sb.append("\n");
        }
        br.close();
        String resp = sb.toString();

        return resp;
    }catch(IOException iOEx){
        return "";
    }
}

2 Comments

Thanks for your answer. Its looking easy for me. But before showing the json data , I must need to fetch the data from php. How can I do that? my php is in localhost (127.0.0.1)
Thanks for your reply. But I said that I am completely new to android/ java. When I try to use your codes , the app crashes and reply "unfortunately <app name> has stopped" .Could you please help me?
1
[ // json array node
    { // json object node 
        "title": "re3",
        "notice": "etrf4w"
    },
    {
        "title": "vfdxg",
        "notice": "fdgd"
    },
    {
        "title": "notice1",
        "notice": "details"
    }
]

To parse

ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
JSOnArray jr = new JSONArray("json string");
for(int i=0;i<jr.length();i++)
{
    JSONObject jb = (JSONObject) jr.getJSONObject(i);
    String title = jb.getString("title");
    String notice =jb.getString("notice"); 
    HashMap<String,String> map = new HashMap<String,String>();
    map.add("key1",title);
    map.add("key2",notice);
    list.add(map);
}

Now you can use the list to display the same in a listview.

Comments

1

Try this..

[ => JSONArray

{ => JSONObject

String response = "Your Response in String format";
JSONArray new_array = new JSONArray(response);
for(int i = 0; i < new_array.lenght; i++){
     JSONObject obj = new_array.getJSONObject(i);
     // You can get Title from obj.getString("title") like below
     System.out.println("Title:"+obj.getString("title"));
     // You can get Notice from obj.getString("notice") like below
     System.out.println("Notice:"+obj.getString("notice"));
}

JSON Parsing turorials

http://www.androidhive.info/2012/01/android-json-parsing-tutorial/

http://androidexample.com/JSON_Parsing_-_Android_Example/index.php?view=article_discription&aid=71&aaid=95

1 Comment

I am new to android/java . So I dont know how to fetch those json data from php .My php code is located at localhost (127.0.0.1). Please help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.