Search code examples
javajsonurlconnection

JSON constructor shown as undefined


JSONObject has an available constructor JSONObject(String s) available, but eclipse is telling me it is undefined.

Documentation

My code is as following:

URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?" +"v=1.0&q=barack%20obama&userip=INSERT-USER-IP");

URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer",  "http://google.com");

String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

while((line = reader.readLine()) != null) {
   builder.append(line);
}

JSONObject json = new JSONObject(builder.toString()); //Error, undefined

I have triple checked that all my libraries are the latest and most stable, as well as implemented properly(apart of my build path).


Solution

  • Did you add it to your build path?

    Here is explained:

    https://stackoverflow.com/a/8997703/3558900