转换Jsoup元素串 [英] Convert Jsoup element to string
本文介绍了转换Jsoup元素串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我开发一个Android应用程序。我需要在一个Jsoup元素更改为字符串,这样我可以减去两个。下面和字符串值code =的valueOf(arrT.val());
不起作用。我不知道如何将元素转换成字符串。我的code是:
I'm developing an Android app. I need to change an Element in Jsoup to a string so that I can subtract the two. The code below and String value = valueOf(arrT.val());
do not work. I'm not sure how to convert an Element into a string. My code is:
TestStation.java
public class TestStation extends Activity {
String URL = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=201412abc85d49b2b83f907f9e329eaa&mapid=40380";
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_station);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Document doc = null;
TextView tv = (TextView) findViewById(R.id.tv);
try {
doc = Jsoup.connect(URL).userAgent("Mozilla/5.0 (Macintosh; U; Intel Mac OS X; de-de) AppleWebKit/523.10.3 (KHTML, like Gecko) Version/3.0.4 Safari/523.10").get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Elements elem = doc.select("eta");
for (Element div : elem) {
}Elements elemn = doc.select("eta"); for (Element div : elem) {
Elements arrT = div.select("arrT");
Elements prdt = div.select("prdt");
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date date2 = sdf.parse(prdt.val());
Date date1 = sdf.parse(arrT.val());
tv.setText(String.valueOf (prdt));
long dateDiff = (date1.getTime() - date2.getTime())>0 ? (date1.getTime() - date2.getTime()) :(date2.getTime() - date1.getTime());
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
sdf1.format(dateDiff);
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
它给了我一个编译错误说PRDT不能从元素更改为字符串。我会爱任何帮助,您可以给这一点,因为在网上注意到我能找到这一点。谢谢你。
It gives me a compile error saying that prdt cannot change from an Element to a string. I would love any help you could give on this, because there is noting online that I can find on this. Thank you.
推荐答案
使用Jsoup的文本()函数
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date date2 = sdf.parse(prdt.text());
这篇关于转换Jsoup元素串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文