I tried to fix this error for 2 days, searched and tried several types of coding from stackoverflow.com. I checked my JSON http://jsonformatter.curiousconcept.com/#jsonformatter . But I still canβt understand why my code is doing this. I have 3 files. MainACtivity.java, which should get information from my test.json files on my server, and then process it in Events.java. Events.java just displays information, but the application does not do it this far.
UPDATED CODE IN THE CASE WHICH A FAVORITE IS NECESSARY FOR USE.
My mistake:
01-14 22:18:08.165: E/JSON Response:(419): > { "event":[
01-14 22:18:08.165: E/JSON Response:(419): {
01-14 22:18:08.165: E/JSON Response:(419): "event_name":"Test Event",
01-14 22:18:08.165: E/JSON Response:(419): "event_time":"7:00pm",
01-14 22:18:08.165: E/JSON Response:(419): "event_price":"$15.00"
01-14 22:18:08.165: E/JSON Response:(419): }
01-14 22:18:08.165: E/JSON Response:(419): ]
01-14 22:18:08.165: E/JSON Response:(419): }
01-14 22:18:08.175: E/Json Error(419): Error: org.json.JSONException: Value [{"event_price":"$15.00","event_time":"7:00pm","event_name":"Test Event"}] at event of type org.json.JSONArray cannot be converted to JSONObject
MainActivity.java
package com.example.dba;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity
{
String event_name, event_time, event_price;
static JSONObject object =null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new PrefetchData().execute();
}
private class PrefetchData extends AsyncTask<Void, Void, Void>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0)
{
JsonParser jsonParser = new JsonParser();
String json = jsonParser.getJSONFromUrl("http://www.website/test.json");
Log.e("JSON Response: ", "> " + json);
if (json != null)
{
try
{
JSONObject parent = new JSONObject(json);
JSONArray eventDetails = parent.getJSONArray("event");
for(int i=0; i < eventDetails.length(); i++)
{
object = eventDetails.getJSONObject(i);
event_name = object.getString("event_name");
event_time = object.getString("event_time");
event_price = object.getString("event_price");
Log.e("JSON", "> " + event_name + event_time + event_price );
}
} catch (JSONException e)
{
Log.e("Json Error", "Error: " + e.toString());
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
Intent i = new Intent(MainActivity.this, Events.class);
i.putExtra("event_name", event_name);
i.putExtra("event_time", event_time);
i.putExtra("event_price", event_price);
startActivity(i);
finish();
}
}
}
}