1

I'm retreving elements in Android with Jsoup. I need to get the next eta element from this code. 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. 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.

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:

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. How can I get the next eta element with the same name? Thank you for your help.

Jonas
  • 121,568
  • 97
  • 310
  • 388
hichris123
  • 10,145
  • 15
  • 56
  • 70
  • 1
    the code i see, doesn't make sense at all. Consider rephrasing your question. – Alécio Carvalho Sep 21 '13 at 22:06
  • My substract, do you mean the mathematical substraction like: 20130921 17:32:24 - 20130921 17:16:24? You have multiple entries of , each containing arrT and prdt, Do you wanna do it for each entry? – Alécio Carvalho Sep 21 '13 at 22:20
  • Yeah, I would want to subtract mathamatically just the time part of 17:32:24 and 17:16:24. I would want to do it for each entry. – hichris123 Sep 21 '13 at 22:23
  • @Alécio With the `while(iterator.hasNext())`, will that still run the code even if there isn't a next element? – hichris123 Oct 05 '13 at 17:34

1 Answers1

1

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.

hichris123
  • 10,145
  • 15
  • 56
  • 70
Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
  • This helps, but how can I physically subtract them? What should I put in my code to subtract the two? – hichris123 Sep 21 '13 at 22:32
  • It depends of the type, you will have to do some work with SimpleDateFormat define the pattern, so it can parse the text into a Date object, and with a Date object you can mathematically subtract the values...It's not complicated, just takes few lines...see this post: http://stackoverflow.com/questions/11446420/parse-string-to-date-java – Alécio Carvalho Sep 21 '13 at 22:41
  • How can I advance to the next eta element? I have the code again, and have changed all the variables, but it displays the same number. @Hi-Tech Android KitKat – hichris123 Sep 24 '13 at 21:06
  • How can I advance to the next eta element? I have the code again, and have changed all the variables, but it displays the same number. @Alecio – hichris123 Sep 25 '13 at 00:39
  • @hichris123 try to use the elem.iterator() (elem => Elements) then you can run the loop over the iterator. http://jsoup.org/apidocs/org/jsoup/select/Elements.html#iterator() – Alécio Carvalho Sep 25 '13 at 08:55
  • If you would, could I have some code for that? I would love it. @Alecio – hichris123 Sep 26 '13 at 20:09
  • well i thought this was clear enough, but anyway, with an Iterator object in hands you can iterate over a collection of elements, in your case, you will be iterating over a collection of objects representing "eta". For some reference on using Iterators in your code, please refer to: http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html – Alécio Carvalho Sep 26 '13 at 23:06
  • I have this on the second part, but it doesn't work. `for (Iterator j = elem.iterator(); j.hasNext(); );; Elements elem1 = doc.select("eta");`. Can I have some help? – hichris123 Sep 26 '13 at 23:54
  • if "elem" is a element...then you can't do the doc.select(eta) on it..of course, so you have to do the: div.select("arrT"); for each element in the iterator. – Alécio Carvalho Sep 26 '13 at 23:59
  • I'll post it to the origional question. – hichris123 Sep 27 '13 at 00:16
  • the code you've posted, made even more confused..try to follow the logic you wrote. Anyway, I updated the answer on how to use the Iterator in this case. – Alécio Carvalho Sep 27 '13 at 00:24
  • That makes a lot of sense. However, it displays a logcat error. It's posted above. – hichris123 Sep 27 '13 at 00:38
  • what's on the line 57 (TestStation.java:57)? Sorry, but i won't be able to help you step by step like this with minor questions. I believe you can take it from here. – Alécio Carvalho Sep 27 '13 at 00:46
  • `Iterator iterator = elem.iterator();` is on line 57. – hichris123 Sep 27 '13 at 00:47
  • then...elem is null. Meaning that doc.select("eta") failed to retrieve the "eta"...."doc" is supposed to be the whole document. Unless if you're doing something different. – Alécio Carvalho Sep 27 '13 at 00:50
  • Hey, just wondering, will the when statment still execute the code if there is no next Element? And if I were to want to see if those strings equaled something, would those strings be empty? – hichris123 Oct 05 '13 at 00:45