8

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

0

2 Answers 2

6

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

Sign up to request clarification or add additional context in comments.

Comments

0

You should try this , it's work...

Set the user-agent header:

.userAgent("Mozilla")

Example

Document document = Jsoup.connect("YOUR_URL").userAgent("Mozilla").get();
Elements elements = document.select("span.hidden-text");
for (Element element : elements) {
  System.out.println(element.text());
}

Comments

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.