Android和桌面应用程序

Android和桌面应用程序

本文介绍了Android和桌面应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不完全知道如何说明问题,但基本上,我使用JSoup解析一些HTML并拉出文章全文。我使用的方法是:

I'm not exactly sure how to describe the problem, but basically, I'm using JSoup to parse some html and pull out the article text. The method I'm using is:

    public static String getArticle(String articleLink) {
    Log.i("article link", articleLink);
    Document doc = null;
    try {
        doc = Jsoup.connect(articleLink).timeout(10000).get();
    } catch (IOException ioe) {
        return null;
    }
    Elements articleBody = doc.select("div.article-body");
    Element first = articleBody.first();
    return first.text();
}

当我拉出code的这个片段中,以及在NetBeans中创建一个示例程序,并通过在链接的网页,它返回的文章就好了。但是,当我在我的Andr​​oid设备上运行它,我在回归first.text()获得一个空指针。

When I pull out this snippet of code, and create a sample program in Netbeans, and pass in the link to the page, it returns the article just fine. But, when I run it on my android device, I get a null pointer at 'return first.text()'.

我不知道这是如何。该应用发布,并一直在努力,但突然间,它开始崩溃,导致我相信的东西在网页的布局改变,但我刚刚运行的独立程序,在相同的articleLink过去了,它的工作原理罚款我的电脑上,但我得到对Android的空指针。 jsoup的版本相同过,任何想法?

I'm not sure how this can be. The app is published and has been working, but all of a sudden, it started crashing, leading me to believe that something changed in the layout of the webpage, but I just ran the standalone program, passed in the same articleLink, and it works fine on my computer, but I get the nullPointer on the android. Same versions of jsoup too, any ideas?

更新:doc变量的值是:

Update: The value of the doc variable is:

<!DOCTYPE html>
<html>
<head>
<title>Redirecting...</title>
<meta http-equiv="refresh"     content="0;url=http://m.ncataggies.com/mobile/ViewArticle.dbml?    atclid=205823481&amp;DB_MENU_ID=&amp;SPSID=&amp;SPID=&amp;DB_OEM_ID=24500" />
<meta name="ROBOTS" content="NOINDEX,NOFOLLOW" />
</head>
<body>
</body>
</html>

因此​​,一些没有改变...

So something did change...

推荐答案

ncataggies.com 服务器检查 User-Agent头从请求,并提供不同的页面,以移动浏览器。因为你不指定一个用户代理,服务器看到默认的代理程序的Andr​​oid设备,这表明它是一个移动浏览器。

The server at ncataggies.com is checking the user-agent header from the request, and serving different pages to mobile browsers. Because you don't specify a user-agent, the server sees the default agent that Android supplies, which identifies it as a mobile browser.

在可以设置用户代理是这样的:

In jsoup you can set the user-agent like this:

String ua = "Mozilla"; // I'd suggest using your current browser as reference
doc = Jsoup.connect(url).userAgent(ua).timeout(10000).get();

您可以检查您的当前用户代理。

You can check your current user-agent here.

这篇关于Android和桌面应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 01:12