简体   繁体   English

获取具有相同名称的Android的下一个Jsoup元素

[英]Get Next Jsoup Element with Same Name Android

I'm retreving elements in Android with Jsoup. 我正在使用Jsoup撤消Android中的元素。 I need to get the next eta element from this code. 我需要从此代码中获取下一个eta元素。 I'm not sure how to. 我不确定该怎么做。 I'm retreiving the xml from: http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=201412abc85d49b2b83f907f9e329eaa&mapid=40380 . 我从以下位置检索xml: http ://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=201412abc85d49b2b83f907f9e329eaa&mapid=40380。 Someone has brought up using elem.iterator(), but I'm not sure how to put this in my code (this is the website for it) http://www.jsoup.org/apidocs/org/jsoup/select/Elements.html#iterator() My code is below. 有人提出使用elem.iterator(),但是我不确定如何将其放在我的代码中(这是其网站) http://www.jsoup.org/apidocs/org/jsoup/select/ Elements.html#iterator()我的代码如下。

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);
TextView tv1 = (TextView) findViewById(R.id.tv1);
TextView tv2 = (TextView) findViewById(R.id.tv2);
TextView tv3 = (TextView) findViewById(R.id.tv3);
TextView tv4 = (TextView) findViewById(R.id.tv4);
TextView tv5 = (TextView) findViewById(R.id.tv5);

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();
}
    Iterator<Element> iterator = elem.iterator();
while(iterator.hasNext())
{ Element div = iterator.next();

Elements arrT = div.select("arrT");
Elements prdt = div.select("prdt");
Elements staNm = div.select("staNm");
String StaNm = staNm.text();
tv1.setText(String.valueOf (StaNm));


while(iterator.hasNext())
{ Element div1 = iterator.next();

Elements arrT1 = div1.select("arrT");
Elements prdt1 = div1.select("prdt");
Elements staNm1 = div1.select("staNm");
String StaNm1 = staNm1.text();
tv2.setText(String.valueOf (StaNm1));



try {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
    Date date1 = sdf.parse(arrT.text());
    Date date2 = sdf.parse(prdt.text());
    Date date3 = sdf.parse(arrT1.text());
    Date date4 = sdf.parse(prdt1.text());

    long dateDiff = (date1.getTime() - date2.getTime())>0 ? (date1.getTime() - date2.getTime()) :(date2.getTime() - date1.getTime());
    long dateDiff1 = (date3.getTime() - date4.getTime())>0 ? (date3.getTime() - date4.getTime()) :(date4.getTime() - date3.getTime());
    SimpleDateFormat sdf1 = new SimpleDateFormat("mm:00");
    String dateDif = sdf1.format(dateDiff);
    String dateDif1 = sdf1.format(dateDiff1);
    tv.setText(String.valueOf (dateDif));
    tv3.setText(String.valueOf (dateDif1));


    }

catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();



 }

}
}
}

I'm now getting LogCat errors, and it says that it has stopped working: 我现在遇到LogCat错误,它说它已经停止工作:

09-26 20:34:50.456: E/AndroidRuntime(22691): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.chicagotraintracker/com.dev.chicagotraintracker.TestStation}: java.lang.NullPointerException
09-26 20:34:50.456: E/AndroidRuntime(22691):    at com.dev.chicagotraintracker.TestStation.onCreate(TestStation.java:57)

With my code, I get two elements, but they are the exact same. 通过我的代码,我得到了两个元素,但是它们是完全相同的。 I get the same numbers for dateDiff and dateDiff1. 我得到相同的数字dateDiff和dateDiff1。 How can I get the next eta element with the same name? 如何获得具有相同名称的下一个eta元素? Thank you for your help. 谢谢您的帮助。

So if I understood correctly, this might help you: 因此,如果我正确理解,这可能会对您有所帮助:

Elements elem = doc.select("eta");
Iterator<Element> iterator = elem.iterator();
while(iterator.hasNext()) {
    Element etaElement = iterator.next();
    Element arrT = etaElement.select("arrT");    
    Element prdt = etaElement.select("prdt");
    //  1. parse the arrT value
    //  2. parse the prdt value
    //  3. subtract them.
}

Like this you will be able to individually handle each entry. 这样,您将能够单独处理每个条目。 Note the the div.select is being used for each eta element and not from the whole document, as your code was doing. 注意, div.select用于每个eta元素,而不是整个文档,就像您的代码一样。

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

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