问题
I want to parse a countdown timer from ebay
<span id="vi-cdown_timeLeft" class="">5g 20h </span>
How can I parse it with jsoup to create a countdown timer on android studio?
Can I parse it like a normal element? Like below
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();
}
}
});
回答1:
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.
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"));
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();
EDIT: 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.
Given that the time you're dealing with is in the following format and not a UNIX timestamp:
Xg Yh Zm
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();
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;
}
Conclusion
- Get the auction time remaining string from ebay (i.e. "3g 10h 5m")
- Call
getMsFromStringon the string from first step - Use the milliseconds remaining in step 2 in the count down timer (remainingMs in the example above)
来源:https://stackoverflow.com/questions/49565617/gather-countdown-timer-with-jsoup-and-setup-a-timer-for-android