3

So I already succeed with one Json to get it all worked. Now I have another class where I want to get only one attribute. I have now a moviedatabase class (which work with JSON and gets all the information) and now I want to add a Trailer which is from Youtube API. so basically I need it to be added into the same JSON to make it easier for me in the future to get it into a HTML. the only problem is I cant get it work. I get a syntax error JSON when using this method.

EDIT CODE 1.1:

Youtube attribute:

public class FilmAttribut {
    private String title = "";
    private String release = "";
    private int vote = 0;
    private String overview = "";
    private String poster = "";
    private String trailer = "";

    // getters + setters stripped    
}

Youtube class:

public class Youtube {

    FilmAttribut movie = new FilmAttribut();
    public void search(String trailer, FilmAttribut in) {

        HttpResponse<JsonNode> response;
        try {
            response = Unirest.get("https://www.googleapis.com/youtube/v3/search?key=[app key here]&part=snippet")
                    .queryString("q", trailer + " trailer")
                    .asJson();

            JsonNode json = response.getBody();
            JSONObject envelope = json.getObject();
            JSONArray items = envelope.getJSONArray("items");

            in.setTrailer("https://youtu.be/" + items.getJSONObject(0).getJSONObject("id").getString("videoId")); //Gives me error here

        }
        catch (JSONException e) {

            e.printStackTrace(); 
        } catch (UnirestException e) {
            e.printStackTrace();
        }

        return null;

    }
}

and main method

public class WebService {

    public static void main(String[] args) {
        setPort(1337);

        Gson gson = new Gson();
        Youtube yt = new Youtube();
        MovieDataBase mdb = new MovieDataBase();




get("/search/:movie", (req, res) ->  {
            String titel = req.params(":movie");
            FilmAttribut film = mdb.searchMovie(titel); 
            yt.search(titel, film);
            String json = gson.toJson(film);
            return json;

        });

So I think the problem is that you can't have two gson.toJson(film) + gson.toJson(trailer); Because it makes the JSON twice, where one time is for the film (aka. movie) and then a new json is created with trailer which make the syntax error.

So my real question is, is it possible to have another class like I have now youtube. to send the information to a attribute class where I have all my attributes and then run it in main-method so that I can get all the JSON in one JSON.

0

1 Answer 1

2

If I did understand well what you are asking, yes you can, but I would do something like that instead:

public void search(String trailer, FileAttribut in) {
    // fetch the trailer from youtube (NB: you should use getters/setters, not public fields)
    in.setTrailer("https://youtu.be/" + items.getJSONObject(0).getJSONObject("id").getString("videoId"));
}

and:

FilmAttribut film = mdb.searchMovie(titel); 
yt.search(titel, film); // pass "film" to "fill" its trailer
return gson.toJson(film);

OR

public String search(String trailer) {
    // fetch the trailer from youtube 
    return "https://youtu.be/" + items.getJSONObject(0).getJSONObject("id").getString("videoId");
}

and:

FilmAttribut film = mdb.searchMovie(titel); 
film.setTrailer(yt.search(titel));
return gson.toJson(film);
Sign up to request clarification or add additional context in comments.

5 Comments

Hmm! I tried to do it but it gave me error on in.trailer where it ask me to create a method for it, I have updated my code as you can see aswell. I have done as you said but the problem is where I said.
Your updated code looks good to me, try recompiling (clearing the project if using eclipse)
It worked after leaning up! Ohhhh my man! Thank you very much! Btw, is there a way to make like ` in.trailer = "youtu.be" + items.getJSONObject(0).getJSONObject("id").getString("videoId");` this into the main method? Or is it harder thing to do?
Not sure what you mean by "main method", I added another possibility as an edit
Got it! It looks very nice, Well I think this is more a class design i'm talking about right now. What I meant is there any possible to make the code better. like instead to have ` in.setTrailer ` in Youtube class. Is it possible to have it in Main-class which I have? And also is it better thing to do or just keep it as I already have? Because next move is take this to HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.