微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Jsoup的android中的网站通知计数器

如何解决使用Jsoup的android中的网站通知计数器

我有一个包含网站的网络视图。 我想从页面提取一个int值。 我要转换的部分的HTML代码是:

<body>
   

     <div class="toolbar" id="main-toolbar">
            <a> <!-- something--> </a>
            <a> <!-- something--> </a>
            <a> <!-- something--> </a>
            <a href="https://thesitethatI'mtalkingabout.com/it/me/notifications" data-unread="1" class="toolbar__item cta-login ">
                    <span class="is-icon-bell toolbar__icon"></span>
                    <span class="toolbar__badge">1</span>
                    "
                                   Notifications
                             "
                    </a> 
    <!--this is the notification number,if you aren't logged in this would be empty--> 
        </div>
    </body>

现在,我将从页面提取值(在本例中为1),并使用它在我的应用程序中创建一个变量。我怎样才能做到这一点? 能够从未读取的数据“ 1”中提取值而不是从跨度中提取值会更好。

我要查找的数字是网站的通知计数。为了使Jsoup能够找到他,他必须先登录。我使用的webView允许您登录。Jsoup是否可以分析网页在HTML中显示的HTML?

解决方法

您可以使用JSoup解析器:

Document doc = Jsoup.connect("someurl").get();
        Log.i("DOC",doc.toString().toString());
        Elements elementsHtml = doc.getElementsByTag("span");  <--- here you specify the html tag where is the text is located
        String[] temp1 = new String[99];    
        int i =0;
        for(Element element: elementsHtml){
            temp1[i] = element.text();
            i++;
        }

您只需遵循CSS selector-syntax来查找元素。文本被解析为字符串。使用Integer,Double等Number类可解析其他格式的值。

要执行登录操作,可以从Connection界面使用其他方法,例如:

  • cookie(),cookies()-添加cookie
  • data()-获取/设置数据
  • execute()-执行请求
  • get()-以GET执行请求并解析结果
  • header(),headers()-添加请求标头
  • method()-设置请求方法
  • post()-以POST形式执行请求并解析结果
  • timeout()-设置请求超时
  • url()-设置请求URLurl
  • userAgent()-设置用户代理

例如,查看以下代码段:

try {
    //grab login form page first
    Response loginPageResponse = 
    Jsoup.connect("someurl")
    .referrer("http://www.rediff.com/")
    .userAgent("Mozilla/5.0")
    .timeout(10 * 1000)
    .get();

    System.out.println("Fetched login page");

    //get the cookies from the response
    Map<String,String> mapLoginPageCookies = loginPageResponse.cookies();

    //make data map containing all the parameters and its values found in the form
    Map<String,String> mapParams = new HashMap<String,String>();
    mapParams.put("FormName","existing");
    mapParams.put("seclogin","on");
    mapParams.put("login","YOUR_USER_ID");
    mapParams.put("passwd","YOUR_PASSWORD");
    mapParams.put("remember","1");
    mapParams.put("proceed","Go");

    //URL found in form's action attribute
    String strActionURL = "https://mail.rediff.com/cgi-bin/login.cgi";

    Response responsePostLogin =
    Jsoup.connect("someurl/login")
     .referrer("someurl/login")//referrer will be the login page's URL
     .userAgent("Mozilla/5.0")
     .timeout(10 * 1000)
    .data(mapParams)//post parameters                    
    .cookies(mapLoginPageCookies)//cookies received from login page
    .post();

    System.out.println("HTTP Status Code: " + responsePostLogin.statusCode());

    //parse the document from response
    Document document = responsePostLogin.parse();
    System.out.println(document);

    //get the cookies
    Map<String,String> mapLoggedInCookies = responsePostLogin.cookies();
} catch (IOException e) {
        // TODO Auto-generated catch block
    e.printStackTrace();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。