I am parsing this JSON data from Rotten Tomatoes website. I am just trying to get the genres array and put it in a toast. But no toast is coming. Here is my code:
public class MovieInfo extends Activity
{
private static final String API_KEY = "xxxxxxxxxxxxxxxxxxxxxxx";
String[] Genres;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.movieinfo);
String MovieID = getIntent().getExtras().getString("MovieID");
new RequestTask().execute("http://api.rottentomatoes.com/api/public/v1.0/movies/"+MovieID+".json?apikey=" + API_KEY);
}
private class RequestTask extends AsyncTask<String, String, String>
{
// make a request to the specified url
@Override
protected String doInBackground(String... uri)
{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try
{
// make a HTTP request
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK)
{
// request successful - read the response and close the connection
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
}
else
{
// request failed - close the connection
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}
catch (Exception e)
{
Log.d("Test", "Couldn't make a successful request!");
}
return responseString;
}
@Override
protected void onPostExecute(String response)
{
super.onPostExecute(response);
if (response != null)
{
try
{
// convert the String response to a JSON object,
// because JSON is the response format Rotten Tomatoes uses
JSONObject jsonResponse = new JSONObject(response);
// fetch the array of movies in the response
JSONArray genres = jsonResponse.getJSONArray("genres");
Genres = new String[genres.length()];
for (int i = 0; i < genres.length(); i++)
{
JSONObject movie = genres.getJSONObject(i);
Genres[i] = movie.getString("genres");
}
// add each movie's title to an array
for(int i = 0; i < genres.length(); i++)
{
Toast.makeText(getBaseContext(), ""+Genres[i], Toast.LENGTH_SHORT).show();
}
}
catch (JSONException e)
{
Log.d("Test", "Failed to parse the JSON response!");
}
}
}
}
}
LogCat Output:
04-12 20:57:46.088: E/AndroidRuntime(593): FATAL EXCEPTION: main
04-12 20:57:46.088: E/AndroidRuntime(593): java.lang.RuntimeException: org.json.JSONException: Value Drama at 0 of type java.lang.String cannot be converted to JSONObject
04-12 20:57:46.088: E/AndroidRuntime(593): at akshat.jaiswal.rottenreviews.MovieInfo$RequestTask.onPostExecute(MovieInfo.java:106)
04-12 20:57:46.088: E/AndroidRuntime(593): at akshat.jaiswal.rottenreviews.MovieInfo$RequestTask.onPostExecute(MovieInfo.java:1)
04-12 20:57:46.088: E/AndroidRuntime(593): at android.os.AsyncTask.finish(AsyncTask.java:602)
04-12 20:57:46.088: E/AndroidRuntime(593): at android.os.AsyncTask.access$600(AsyncTask.java:156)
04-12 20:57:46.088: E/AndroidRuntime(593): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
04-12 20:57:46.088: E/AndroidRuntime(593): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 20:57:46.088: E/AndroidRuntime(593): at android.os.Looper.loop(Looper.java:154)
04-12 20:57:46.088: E/AndroidRuntime(593): at android.app.ActivityThread.main(ActivityThread.java:4624)
04-12 20:57:46.088: E/AndroidRuntime(593): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 20:57:46.088: E/AndroidRuntime(593): at java.lang.reflect.Method.invoke(Method.java:511)
04-12 20:57:46.088: E/AndroidRuntime(593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
04-12 20:57:46.088: E/AndroidRuntime(593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
04-12 20:57:46.088: E/AndroidRuntime(593): at dalvik.system.NativeStart.main(Native Method)
04-12 20:57:46.088: E/AndroidRuntime(593): Caused by: org.json.JSONException: Value Drama at 0 of type java.lang.String cannot be converted to JSONObject
04-12 20:57:46.088: E/AndroidRuntime(593): at org.json.JSON.typeMismatch(JSON.java:100)
04-12 20:57:46.088: E/AndroidRuntime(593): at org.json.JSONArray.getJSONObject(JSONArray.java:484)
04-12 20:57:46.088: E/AndroidRuntime(593): at akshat.jaiswal.rottenreviews.MovieInfo$RequestTask.onPostExecute(MovieInfo.java:91)
04-12 20:57:46.088: E/AndroidRuntime(593): ... 12 more
I am not able to parse objects like Title or release, is there something I am missing here?