I just download the latest version of jsoup(1.7.1) and follow the official code(changed the url). Then i got "http error fetching url"
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
loadData();
} catch (IOException e) {
Log.i("error",e.getMessage());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void loadData() throws IOException {
Document doc = Jsoup.connect("http://forum.mtr.com.hk/search.php?station=30&cat=&x=25&y=2").get();
String title = doc.title();
Log.i("title",title);
}}
What's the problem of my code? It seems the error just happend in Android Project since i do the same thing in a Java Project that works fine.
Notes: - I haved added the Internet permission
I had a similar problem. Could be that your device connection is too slow and the connect() function times-out too soon, or maybe it is failing with some harmless HTTP error status. Also, my Jsoup connection worked for most pages, but for some I was getting "HTTP error fetching URL. Status=307". Turns out this is really a redirect request from the web server, to another URL. I solved all these problems with the following code:
Connection.Response res = Jsoup.connect(sUrl).
timeout(5000).ignoreHttpErrors(true).followRedirects(true).execute();
if (res.statusCode() == 307) {
String sNewUrl = res.header("Location");
if (sNewUrl != null && sNewUrl.length() > 7)
sUrl = sNewUrl;
res = Jsoup.connect(sUrl).
timeout(5000).execute();
}
Document doc = res.parse();
Hope this helps, or at least inspires you to try a few more settings before calling get() or execute().
Greg
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With