0

我正在解析来自烂番茄网站的 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 等对象,这里有什么我遗漏的吗?

4

1 回答 1

-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);
}
于 2014-09-24T17:42:32.850 回答