我正在开发一个 Android 应用程序。我需要将 Jsoup 中的 Element 更改为字符串,以便可以将两者相减。下面的代码 String value = valueOf(arrT.val());
不起作用。我不确定如何将元素转换为字符串。我的代码是:
测试站.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 不能从元素更改为字符串。我希望您能对此提供任何帮助,因为我可以在网上找到关于此的注释。谢谢你。