1

我正在使用 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() 我的代码如下。

测试站.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 元素?谢谢您的帮助。

4

1 回答 1

1

因此,如果我理解正确,这可能会对您有所帮助:

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元素而不是整个文档。

于 2013-09-21T22:28:40.747 回答