简体   繁体   English

将Jsoup元素转换为字符串

[英]Convert Jsoup element to string

I'm developing an Android app. 我正在开发一个Android应用程序。 I need to change an Element in Jsoup to a string so that I can subtract the two. 我需要将Jsoup中的Element更改为字符串,以便可以减去两者。 The code below and String value = valueOf(arrT.val()); 下面的代码和String value = valueOf(arrT.val()); do not work. 不工作。 I'm not sure how to convert an Element into a string. 我不确定如何将Element转换为字符串。 My code is: 我的代码是:

TestStation.java 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();



 }

}
}   

It gives me a compile error saying that prdt cannot change from an Element to a string. 它给了我一个编译错误,说prdt无法从Element更改为字符串。 I would love any help you could give on this, because there is noting online that I can find on this. 我很乐意为您提供任何帮助,因为在网上可以找到关于此的信息。 Thank you. 谢谢。

Use Jsoup text() function 使用Jsoup text()函数

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date date2 = sdf.parse(prdt.text());

try out: 试用:

String value = arrT.val(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

Date date = sdf.parse(value);

see the jsoup apidocs 请参阅jsoup apidocs

Note: the date pattern must match with the format you are passing to the parse method. 注意:日期格式必须与您传递给parse方法的格式匹配。

Additional note: as doc = Jsoup.connect(URL) does network calls, consider putting it in an AsyncTask instead of calling directly on the UI Thread (in the onCreate() method). 附加说明:由于doc = Jsoup.connect(URL)进行网络调用,请考虑将其放在AsyncTask中,而不是直接在UI线程上(在onCreate()方法中)进行调用。

If you are getting date in 如果你要约会

Then it should be like this and are you forgetting prdt.val() 然后应该是这样,您是否忘记了prdt.val()

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
Date date = sdf.parse(prdt.val());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM