0

i use Jsoup to extract specified data from a website,

try{
   Document doc = Jsoup.connect("http://example/search/").get();
} catch(IOException){
  System.out.println("error");
}

but i'm got failed, and the output is "error".

when i browse with Mozilla,or another browser this address is successfully to load. Any idea?Please help me..

Best regards

enhaka
  • 85
  • 8
  • THe output *of your own error message* is 'error'. If you printed something more useful, such as the exception itself, or its stack trace, you wouldn't be so baffled, and someone else might be able to answer this question. As it is, we can't. When you get an exception, don't just print some message of your own devising and then wonder what happened. – user207421 Mar 14 '14 at 05:46
  • ok @EJP i will remember your advice, thank you – enhaka Mar 14 '14 at 12:21

1 Answers1

1

If you display the exception message from your IOException message, you will see

org.jsoup.HttpStatusException: HTTP error fetching URL. Status=500, URL=...

Solution: You need to set the user agent to correspond to the mobile website

Document doc = 
     Jsoup.connect("http://m.tokobagus.com/search/province").userAgent
      ("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko)          
        Chrome/15.0.874.120 Safari/535.2").get();

More importantly, remember to display those exception messages:

} catch(IOException ioe){
  ioe.printStacktrace();
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276