0

im trying to convert a json object to a string by using gson here is some of my code

public void returnJson(){

    TextView one = (TextView) findViewById(R.id.textView1);

    try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_list.php");

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e) {
        one.setText("error3");
    }

    try{


        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);                      
        StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

    }catch(Exception e) {
        one.setText("error2");          
        }

    try{
        JSONArray jArray = new JSONArray(result);
        for(int i = 0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(0);
            //Log.i("log_tag","story_name: "+json_data.getString("story_name") );
            result += "\n" + jArray.getJSONObject(i); 

        }
        one.setText(result);
    }
    catch(JSONException e) {
        one.setText("error1");
    }
        return;


//end of returnJson()   
}

this is what is outputs into the text view

[{"story_name":"Story One"},{"story_name":"Story Two"},{"story_name":"Story Three"},{"story_name":"Story Four"},{"story_name":"Story Five"},{"story_name":"Story Six"}]

{"story_name":"Story One"}
{"story_name":"Story Two"}
{"story_name":"Story Three"}
{"story_name":"Story Four"}
{"story_name":"Story Five"}
{"story_name":"Story Six"}

exactly like that, i need to know why it outputs it twice and which codes cause each output, then i also need to know how to change this with gson so it outputs

Story One
Story Two
Story Three
Story Four
Story Five
Story Six
daniel
  • 123
  • 1
  • 4
  • 13
  • This can help you a bit http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android – Relsell Feb 14 '12 at 06:33

1 Answers1

2

First, this line seems to have no purpose....

JSONObject json_data = jArray.getJSONObject(0);

The output appears twice because the line

[{"story_name":"Story One"},{"story_name":"Story Two"},{"story_name":"Story Three"},{"story_name":"Story Four"},{"story_name":"Story Five"},{"story_name":"Story Six"}]

is already in the result String when you start "appending" the others.

If the output is as simple as this, all you need to do is :

String storyNames = "";
for(int i = 0;i<jArray.length();i++){
        storyNames += jArray.getJSONObject(i).getString("story_name"); 
}

You don't really need Gson for something so simple.

Saad Farooq
  • 13,172
  • 10
  • 68
  • 94
  • thanks saad should i add "\n" if im going to use this in a listView? – daniel Feb 14 '12 at 06:43
  • it semi works but now it outputs story oneStory TwoStory Three etc, i tryed adding "\n" storyNames += "\n" jArray.getJSONObject(i).getString("story_name"); but that displays nothing – daniel Feb 14 '12 at 06:46
  • how do i get the names seperated – daniel Feb 14 '12 at 06:48
  • Whoa... using it in a ListView is a total different matter. That's different... but `storyNames += jArray.getJSONObject(i).getString("story_name") + "\n";` should work for this. – Saad Farooq Feb 14 '12 at 06:48
  • awsome its working, i know how to use listviews dont i just add the string into it? – daniel Feb 14 '12 at 06:53
  • For the ListView, you need to have an Adapter that has the data you want to show in the List. In your case, a String array might be a good dataset in which case you might actually use Json and do something like this : `String[] s = new Gson().fromJson(jarray,String[].class);` then use that in your Adapter – Saad Farooq Feb 14 '12 at 08:43
  • i used listview and adapters in my menus i was hoping it was just like what the documentation says about how to create a listview, mate your time has really helped me out alot!!! i have a bit of a complex sql DB for the pages and stories, so i guess this should work with that aswell – daniel Feb 14 '12 at 11:07