Nichs Nichs - 4 months ago 15
JSON Question

Parsing Json Data in Android Studio - cannot display all data in logs

So basically I am developing an app which retrieves data from mysql database. Therefore I have created a php script which retrieves data from database and outputs data in JSON format:

{
"newslog": [
{
"Id": "1",
"Title": "News Item 1",
"Author": "Nich",
"Body": "aldfjlak dklflksd afkjdflk ",
"Date": "2016-07-26"
},
{
"Id": "2",
"Title": "News Item 2",
"Author": "nich",
"Body": "la la la la la la",
"Date": "2016-07-26"
},
{
"Id": "3",
"Title": "News Item 3",
"Author": "nich",
"Body": "oh oh oh oh oh",
"Date": "2016-07-26"
}
]
}


I am using Android Studio, and managed to display this data (in JSON format) in Android Studio logs, as shown below:

enter image description here

Problem:

I would like to display parts from this data (JSON) in the logs. However at the moment I can only display in the logs the "Title". When I try to retrieve the "Id", "Body" or "Date" they seem to be ignored. I am sure that I am missing something.

Code used to output data in logs:

Code used to output data in logs

Logs:

enter image description here

Android Studio code:

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class NewsActivity extends AppCompatActivity {

private ListView lv;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
lv = (ListView) findViewById(R.id.listView);


// List<String> newsItems = new ArrayList<String>();
// newsItems.add("News 1");
// newsItems.add("News 2");
//
//
// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, newsItems);
// lv.setAdapter(arrayAdapter);

DownloadTask task = new DownloadTask();
task.execute("http://council4u.com/php-scripts/getnews.php");
}

public class DownloadTask extends AsyncTask<String, Void, String>{

@Override
protected String doInBackground(String... urls) {

String result = "";
URL url;
HttpURLConnection urlConnection = null;

try {
url = new URL(urls[0]);

urlConnection = (HttpURLConnection) url.openConnection();

InputStream in = urlConnection.getInputStream();

InputStreamReader reader = new InputStreamReader(in);

int data = reader.read();

while (data != -1) {

char current = (char) data;

result += current;

data = reader.read();
}

return result;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

try{
JSONObject data = new JSONObject(result);

String latestnews = data.getString("newslog");
Log.i ("Latest News", latestnews);


JSONArray arr = new JSONArray(latestnews);

for (int i = 0; i < arr.length(); i++) {

JSONObject jsonPart = arr.getJSONObject(i);


Log.i("title", jsonPart.getString("Title"));
Log.i("Body", jsonPart.getString("Body"));

}

} catch (JSONException e) {
e.printStackTrace();
}


}}
}

Answer

EDIT: you are using :

 String latestnews = data.getString("newslog");

but in the response it is an array, try to replace those lines :

String latestnews = data.getString("newslog");
JSONArray arr = new JSONArray(latestnews);

By :

JSONArray arr = data.getJSONArray("newslog");

when the message to Log is empty there is no log message, try to log the length of the body and test if it is null :

  String body = jsonPart.getString("Body");
  Log.i("title", jsonPart.getString("Title"));
  Log.i("Body", body==null? "null body" : body.length());