Sorry for my newbie question, but please tell me what I do wrong. I'm trying to parse JSON from URL which contains such strings as :
[{"Date":"25","Time":"10:00","Place":"","Text":"\u0414\u041e\u0411\u0420\u041e \u041f\u041e\u0416\u0410\u041b\u041e\u0412\u0410\u0422\u042c \u0412 \u00ab\u041f\u0410\u0420\u041a \u0414\u0417\u042e\u0414\u041e\u00bb!"},{"Date":"25","Time":"10:00","Place":"\u041a\u0440\u044b\u0448\u0430 \u041b\u0410 \u00ab\u0422\u0420\u0410\u041a\u0422\u041e\u0420\u00bb","Text":"Open-Air: \u0432\u0430\u0441 \u043f\u0440\u0438\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0434\u0438-\u0434\u0436\u0435\u0439 \u041f\u0430\u0440\u043a\u0430 \u0414\u0417\u042e\u0414\u041e DJ \u0420\u043e\u043c\u0430\u043d \u041a\u0438\u0442\u0430\u0435\u0446"}]
As you can see this is an JSON array with no name and with objects inside. So how can I parse it to show in my ListView? What I do :
public class NewsActivity extends ListActivity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "http://mysitehere.com/news.php";
// JSON Node names
private static final String TAG_DATE = "Date";
private static final String TAG_TIME = "Time";
private static final String TAG_PLACE = "Place";
private static final String TAG_TEXT = "Text";;
// Hashmap for ListView
ArrayList<HashMap<String, String>> AfishaList;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.news_activity);
new GetInfo().execute();
}
private class GetInfo extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("LOG_TAG","PROGRESS STARTS");
// Showing progress dialog
pDialog = new ProgressDialog(NewsActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
//JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray mJSONArray = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < mJSONArray.length(); i++) {
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
JSONObject e = mJSONArray.getJSONObject(i);
// adding each child node to HashMap key => value
contact.put(TAG_DATE, e.getString("Date"));
contact.put(TAG_TIME, e.getString("Time"));
contact.put(TAG_PLACE, e.getString("Place"));
contact.put(TAG_TEXT, e.getString("Text"));
// adding contact to contact list
AfishaList.add(contact);
Log.d("LOG_TAG","SOMETHING IS GOING ON");
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
NewsActivity.this, AfishaList,
R.layout.list_item, new String[] { TAG_DATE, TAG_TIME,
TAG_PLACE, TAG_TEXT }, new int[] { R.id.date,
R.id.time, R.id.place, R.id.text });
setListAdapter(adapter);
Log.w("FINAL","!!!");
}
}
}
EDIT:
I've changed the method in doInBackground
and now logcat
shows me the following log:
07-31 10:05:35.682 3920-3934/com.project1.2014 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.project1.2014.NewsActivity$GetInfo.doInBackground(NewsActivity.java:110)
at com.project1.2014.NewsActivity$GetInfo.doInBackground(NewsActivity.java:65)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)