Search code examples
javaandroidrotten-tomatoes

Error converting String to JSONArray while using Rotten Tomatoes API developing for Android


While using the Rotten Tomatoes API I am getting the following error:

org.json.JSONException: Value 596 of type java.lang.String cannot be converted to JSONArray.

I am attempting to parse out data from the Rotten Tomatoes API which is in JSON format.

Here is what my code looks like.

package com.my.apitest;

import java.io.*;
import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.json.*;
import org.apache.http.impl.client.*;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity {

    EditText display;
    HttpResponse responseGet;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            display = (EditText) findViewById(R.id.textBox);

            String result = "";

            try {
            HttpClient client = new DefaultHttpClient();
            String movieID = "770672123";

            HttpPost getCast = new HttpPost("http://api.rottentomatoes.com/api/public/v1.0/movies/" +
                                            movieID + "/cast.json?apikey=3p9ehnhzbxwpbd6mk8fncf67");

            HttpResponse responseGet = client.execute(getCast);
            HttpEntity resEntityGet = responseGet.getEntity();

            InputStream webs = resEntityGet.getContent();

            // (i was converting to string like this)display.setText(EntityUtils.toString(resEntityGet));

            try{
                BufferedReader reader = new BufferedReader (new InputStreamReader (webs, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                webs.close();
                result=sb.toString();
            }catch(Exception e 
                Log.e("log_tag", "Error converting result: " + e.toString());

            }
        }catch(Exception e 
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        try{
            JSONArray jArray = new JSONArray(result);

            for (int i=0; i<jArray.length(); i++ 
                JSONObject json_data = jArray.getJSONObject(i);
                display.setText(json_data.getString("name"));
            }
        }
        catch(JSONException e 
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
    }   
    catch (Exception e 
        Log.e("Error", "Error in Code:" + e.toString());
        e.printStackTrace();}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return false;
    }
}

After doing some digging I have come across some information suggestion I need to connect to the api with PHP. I am new to APIs and mobile development so I am not sure how to go about that or if that is in fact the issue. I think what may be happening is the error is being raised due to the absence of a PHP file to connect to the API and decode the JSON. Does that sound right? Any help would be greatly appreciated.


Solution

  • If your response start with [ Square brecket than it indicate JSONArray than you have to use

    JSONArray  jsonarray=new JSONArray(response);
    

    and if it is start with { Curly bracket than it indicate JSONObject than you have to use

    JSONObject jsonobject=new JSONObject(response);
    

    see more details about json at http://www.json.org/ and example for JSON parsing in android

    Android JSONParsing Tutorial 1

    Android JSONParsing Tutorial 2