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
First, this line seems to have no purpose….
The output appears twice because the line
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 :
You don’t really need Gson for something so simple.