从直接链接解析Jsoup不起作用 [英] Jsoup parsing from direct link doesn't work

查看:71
本文介绍了从直接链接解析Jsoup不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从网站接收内容.总的来说,它很好用,但我对其他一个有问题.网页具有主页,其中一张图片位于中间,单击该图片后,将我们移至另一页.我尝试显示直接链接中的内容,但始终从该主页而不是我想要的页面中接收内容.我使用jsoup库.有没有可能解决这个问题?我的代码:

I need to receive content from website. Generally it works good but I have problem with other one. Webpage has main page where one picture is in the middle and after clicking it, moves us to the another page. I try to show content from direct link but always I receive content from this main page not from page which I want. I use jsoup library. Is there any possibility to solve this? My code:

private class Parser extends AsyncTask<Void, Void, Void> {
    String h;
    String url = "http://www.klt.net.pl/index.php?a=ostatnie_kolejki";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd = new ProgressDialog(MainActivity.this);
        pd.setTitle("Parser");
        pd.setMessage("Loading...");
        pd.setIndeterminate(false);
        pd.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            Document document = Jsoup.connect(url).get();
            h = document.html();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        r.setText(h);
        pd.dismiss();
    }
}

推荐答案

我尝试访问您的URL,通过检查网络标签,我发现您要访问的页面需要一个PHPSESSIONID,这是来自的响应cookie带有图像的页面.所以请尝试下面的代码,它应该可以工作:)

I have tried accessing your URL, and by inspecting the network tab I found out that the page you want to access requires a PHPSESSIONID, which is a response cookie from the page with the image. So please try the code below, it should work :)

@Override
protected Void doInBackground(Void... params) {
    try {
        Connection.Response response = Jsoup.connect(url)
            .method(Connection.Method.GET)
            .timeout(50000)
            .followRedirects(true)
            .execute();
        Document document = Jsoup.connect("http://www.klt.net.pl/index.php")
            .cookies(response.cookies())
            .get();
        h = document.html();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

这篇关于从直接链接解析Jsoup不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆