我正在从Rotten Tomatoes website解析此JSON数据。我只是试图获得类型数组并将其置于吐司中。但没有吐司即将到来。这是我的代码:
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输出:
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
我无法解析Title或release这样的对象,我在这里缺少什么?
答案 0 :(得分:-1)
尝试类似:
// convert the String response to a JSON object,
// because JSON is the response format Rotten Tomatoes uses
JSONObject jsonResponse = new JSONObject(response);
//Array
JSONArray genres = jsonResponse.getJSONArray("genres");
// which gives you this:
// "genres": [
// "Animation",
// "Kids & Family",
// "Science Fiction & Fantasy",
// "Comedy"
// ]
//Now you can ask length
int howManyGenres = genres.length();
String[] genresStr = new String[howManyGenres];
//You can iterate here
for(int i=0; i<howManyGenres; i++) {
genresStr[i] = genres.get(i);
}