Search code examples
javaandroidhtml-parsingjsoupuser-agent

How to set an user agent and connection timeout for jsoup in android


I was trying to extract some tweets from mobile.twitter.com in an android, and as a result I get a mixed html document. After some searches I realized that I need to set an user agent.

My aim is to set a default user agent which would work not only for me, but also for other users who would use my application.

Document doc = Jsoup.connect("https://mobile.twitter.com/").userAgent(...).get();

Solution

  • For those who might be interested there is an easy way for getting default user agent if you are on Android 2.1 or above.

    There is a system property called http.agent, which can be used to retrieve the User-Agent string.

    My code would then turn to:

    String userAgent = System.getProperty("http.agent");
    Document doc = Jsoup.connect("https://mobile.twitter.com/").userAgent(userAgent).get();