获取下一个同名的Jsoup元素Android

xurqigkl  于 2023-06-04  发布在  Android
关注(0)|答案(1)|浏览(239)

我正在用Jsoup恢复Android中的元素。我需要从这个代码中得到下一个eta元素。我不知道该怎么做。我正在从以下位置检索XML:http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=201412abc85d49b2b83f907f9e329eaa&mapid=40380。有人提出使用elem.iterator(),但我不确定如何将其放入我的代码中(这是它的网站)http://www.jsoup.org/apidocs/org/jsoup/select/Elements.html#iterator()我的代码如下。
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();


 }

}
}
}

我现在得到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)

在我的代码中,我得到了两个元素,但它们完全相同。dateDiff和dateDiff1得到相同的数字。我如何得到下一个具有相同名称的eta元素?谢谢你的帮助。

k10s72fa

k10s72fa1#

如果我理解正确的话,这可能会对你有所帮助:

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.
}

这样,您就可以单独处理每个条目。请注意,div.select用于每个eta元素,而不是像您的代码那样用于整个文档。

相关问题