skip to Main Content

Would be nice if someone could help me solve this problem.
I try to load an image from TMDB with the folowing code:

imports:

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONObject;

Method:

public static void getImageFromTMDB(String title) {
    try {
        String apiKey = "47f285ae13541c6a4e42914c1c8f7ef9";
        String urlString = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&query=" + title;
        URL url = new URL(urlString);
        // Send the request and receive a JSON response
        String json = IOUtils.toString(url, StandardCharsets.UTF_8);

        JSONObject jsonObject = new JSONObject(json);
        JSONObject results = jsonObject.getJSONObject("results");
        String posterPath = results.getString("poster_path");

        // Construct the complete URL for the poster image
        String imageUrl = "https://image.tmdb.org/t/p/w500" + posterPath;
        // Read image from the URL
        BufferedImage image = ImageIO.read(new URL(imageUrl));
        // Save image to local directory
        File outputFile = new File("src/main/resources/posters/" + title + ".jpg");
        ImageIO.write(image, "jpg", outputFile);
    } catch (IOException e) {
        System.out.println("Poster not found");
    }
}

but i get these error messages:

Cannot resolve method ‘getJSONObject’ in ‘JSONObject’

Cannot resolve method ‘getString’ in ‘JSONObject’

Image of Message

my pom file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>groupId</groupId>
    <artifactId>movieProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>19</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-base</artifactId>
            <version>19</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20220924</version>
        </dependency>
    </dependencies>
</project>

What do I do wrong?

I tried different versions of json but was’t successful so far.

thank’s a lot 🙂

Best Wishes Cerberos

2

Answers


  1. Just replace this line:

    import org.json.simple.JSONObject;
    

    to this:

    import org.json.JSONObject;
    
    Login or Signup to reply.
  2. Yeap, there are no such methods defined on the version of the lib that you are using.

    Maybe this will work

    SONObject jsonObject = (JSONObject) JSONValue.parse(json);
    JSONObject results = (JSONObject) jsonObject.get("results");
    String posterPath = (String) results.get("poster_path");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search