2

I want to get movie data from the OMDB API which is JSON text. I am using Java to decode this and the package JSON-simple.

The URL I want to decode is this for example: http://www.omdbapi.com/?t=icarus

Outcome (directly copy and paste, not structured):

{"Title":"Icarus","Year":"2010","Rated":"TV-14","Released":"10 Dec 2010","Runtime":"42 min","Genre":"Adventure, Drama, Romance","Director":"Mairzee Almas","Writer":"Alfred Gough (creator), Jerry Siegel (character created by: Superman), Miles Millar (creator), Joe Shuster (character created by: Superman), Alfred Gough (developed for television by), Miles Millar (developed for television by), Genevieve Sparling","Actors":"Tom Welling, Erica Durance, Cassidy Freeman, Justin Hartley","Plot":"As the VRA threat intensifies, Clark takes initiative by closing down watchtower and declaring the League go officially underground, but will this be enough to stop trotter and Slade Wilson...","Language":"English","Country":"USA","Awards":"N/A","Poster":"http://ia.media-imdb.com/images/M/MV5BMjIwNDQ2MjM5OV5BMl5BanBnXkFtZTcwODU4NzU0NA@@._V1_SX300.jpg","Metascore":"N/A","imdbRating":"8.6","imdbVotes":"367","imdbID":"tt1628582","Type":"episode","Response":"True"}

The code under my button:

        String url = "http://www.omdbapi.com/?t=" + jListFilms.getSelectedValue().toString();

    try {
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(url);
        JSONObject jsonObj = (JSONObject) obj;

        String title = (String) jsonObj.get("Title") ;
        System.out.println(title);

    } catch (ParseException e){
        System.out.println(e);
    }

When I print out the variable title

Unexpected character (h) at position 0.

Does anyone know why I don't get the title of the movie?

Bilzard
  • 371
  • 1
  • 5
  • 19

2 Answers2

1

What your code is doing is parsing the string URL which starts "http://", hence the h at location 0.

You need to issue an HTTP GET request at that url in order to get the JSON back.

Here's an answer for how to issue the GET request

Community
  • 1
  • 1
Adam Yost
  • 3,616
  • 23
  • 36
1

Thanks Adam for leading me into the right direction. I am using InputStream and Google Gson instead of JSON.Simple now.

This is my code now and it works how I wanted it to work.

        try {

        String selectedItem = jListFilms.getSelectedValue().toString().replace("\\s+", "+");

        InputStream input = new URL("http://www.omdbapi.com/?t=" + URLEncoder.encode(selectedItem, "UTF-8")).openStream();
        Map<String, String> map = new Gson().fromJson(new InputStreamReader(input, "UTF-8"), new TypeToken<Map<String, String>>(){}.getType());

        String title = map.get("Title");
        String year = map.get("Year");
        String released = map.get("Released");
        String runtime = map.get("Runtime");
        String genre = map.get("Genre");
        String actors = map.get("Actors");
        String plot = map.get("Plot");
        String imdbRating = map.get("imdbRating");
        String poster = map.get("Poster");

        testForm tf = new testForm(title, year, released, runtime, genre, actors, plot, imdbRating);
        tf.setVisible(true);

    } catch (JsonIOException | JsonSyntaxException | IOException e){
        System.out.println(e);
    }
Bilzard
  • 371
  • 1
  • 5
  • 19