使用jsoup收集倒数计时器并为Android设置计时器 [英] Gather countdown timer with jsoup and setup a timer for android

查看:53
本文介绍了使用jsoup收集倒数计时器并为Android设置计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析eBay的倒数计时器

I want to parse a countdown timer from ebay

<span id="vi-cdown_timeLeft" class="">5g 20h </span>

我如何用jsoup解析它以在android studio上创建一个倒数计时器?

How can I parse it with jsoup to create a countdown timer on android studio?

我可以像普通的element一样解析吗?像下面一样

Can I parse it like a normal element? Like below

更新: getMsFromString与shn android dev的下面编写的方法相同

Update: the getMsFromString is the same method written by below from shn android dev

public synchronized void getTimer() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                sem.acquire();

                Document doc = Jsoup.connect(linkurl).get();
                remaining = doc.select("#vi-cdown_timeLeft").first().text();
                msFromString = getMsFromString(remaining);
                long remainingMs = System.currentTimeMillis() - msFromString;
                new CountDownTimer(remainingMs, 1000) {

                    public void onTick(long millisUntilFinished) {
                        timer.setText("seconds remaining: " + millisUntilFinished / 1000);
                    }

                    public void onFinish() {
                        timer.setText("done!");
                    }

                }.start();
                sem.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }



        }
    });

推荐答案

因此,与倒数计时器关联的ebay网页的HTML部分如下所示:

So the HTML part of the ebay webpage associated with the countdown timer looks like this:

<span class="vi-tm-left">
     <span class="timeMs" timems="1522415936000">Friday, 8:18AM</span>
</span>

您想使用

You want to get the value of the span element with the "timems" attribute. Note, that timems is the number of milliseconds elapsed since Jan 01, 1970.

在您的JSoup中,尝试执行以下代码以获取timems的值(您需要将URL更改为eBay出价所在的位置!):

In your JSoup try executing the following code to get the value of timems (you need to change the URL to where your eBay bid is located!):

Document doc = Jsoup.connect("http://myebayurl.com/").get();
Element timeSpanEle = doc.select("span.timeMs").first();
long timeMs = Long.parseLong(timeSpanEle.attr("timems"));

现在,我们有了Unix时间戳记的值(以毫秒为单位,而不是秒!),指示拍卖何时到期.现在,我们需要在Android Studio上设置一个倒数计时器.我们可以使用android.os.CountDownTimer来执行此操作,因为您尚未指定要使用的类(但是这应该使您知道如何执行该操作).

Now we have the value of the Unix timestamp (in milliseconds, not seconds!) indicating when the auction expires. Now we need to make a countdown timer on Android Studio. We can use the android.os.CountDownTimer to do this, since you haven't specified a class you'd like to use (however this should give you an idea how to do it).

long remainingMs = System.currentTimeMillis() - timeMs;
new CountDownTimer(remainingMs, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }

}.start();

因此,原始帖子中的内容已发生了一些变化.根据您的拍卖,HTML和剩余的拍卖时间不是UNIX时间戳,而是String值,因此先前的答案对您不起作用.这是因为Ebay网站的意大利语版本(一个OP正在使用)与美国版本(我错误地假定OP正在使用的版本)不同.这是更新的答案.

So the content in the original post has changed a little bit. The HTML with the time remaining for the auction is not a UNIX timestamp according to you but a String value, and thus the previous answer will not work for you. This is because the Italian version of the Ebay website (the one OP is using) is different from the American one (the version I erroneously assumed OP was using). Here is an updated answer.

鉴于您要处理的时间采用以下格式,而不是UNIX时间戳:

Given that the time you're dealing with is in the following format and not a UNIX timestamp:

Xg Yh Zm

Xg Yh Zm

其中X是天数(g),Y是小时数(h),Z是分钟数(m).

Where X is the number of days (g), Y is the number of hours (h) and Z is the number of minutes (m).

我们通过以下方式获得剩余时间的值:

We get the value of the time remaining by the following:

Document doc = Jsoup.connect("http://myebayurl.com/").get();
String remaining = doc.select("#vi-cdown_timeLeft").first().text();

我们现在需要解析此String并将其转换为剩余的ms.从上面获取剩余的String时间,并使用以下静态方法将其解析为ms:

We now need to parse this String and convert it into ms remaining. Take the time remaining String from above and parse it into ms with the following static method:

public static long getMsFromString(String str) {
    String[] arr = str.split(" ");
    long auctionTimeLeft = 0;
    for(String s : arr){
        if(s.contains("g")) { //convert days to milliseconds
            auctionTimeLeft += Long.parseLong(s.substring(0, s.indexOf("g"))) * 8.64e7;
        }
        else if(s.contains("h")){ //convert hours to milliseconds
            auctionTimeLeft += Long.parseLong(s.substring(0, s.indexOf("h"))) * 3.6e6;
        }
        else if(s.contains("m")){ //convert minutes to milliseconds
            auctionTimeLeft += Long.parseLong(s.substring(0, s.indexOf("m"))) * 60000;
        }
    }
    return auctionTimeLeft;
}

结论

  1. 从ebay获取剩余的拍卖时间字符串(即"3g 10h 5m")
  2. 从第一步开始对字符串调用getMsFromString
  3. 使用倒数计时器中步骤2剩余的毫秒数(在上面的示例中为Ms)

这篇关于使用jsoup收集倒数计时器并为Android设置计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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