Search code examples
javaandroidjsoup

Jsoup, java, android studio - no results


I am currently writing an android app in android studio. I would like to connect with gog.com and search for a specific game. I have done this with steam and it works fine. When I want to search for a game at https://www.gog.com/games?page=1&search=game_title&sort=popularity it does not work.

Piece of my code:

String search ="https://www.gog.com/games?page=1&search=cyberpunk&sort=popularity";
Document doc = Jsoup.connect(search).get();
Elements titles = doc.getElementsByClass("product-tile__title");
for(Element title: titles){
    txt = title.text();
    Log.i("My_title", txt);
}

I wrote "cyberpunk" by default. This link works in a browser, but when I use Jsoup the same link returns "No results found". Is it a problem with cookie/user-agent/language ?


Solution

  • Well, you're right, your code should be working but in this particular case it will not work.

    The reason is because if you disable javascript on your browser, and make this GET request https://www.gog.com/games?page=1&search=cyberpunk&sort=popularity you will see that the page will not load, and the reason is because these html elements are constructed or loaded by a javascript code.

    So in this case you will have to parse the json that you received in your response instead of using JSOUP.

    Conclusion

    Because JSOUP can't run javascript as it's not a webbrowser, you will not be able to parse the HTML response in this case, but instead, parse the JSON in the HTML response.