2

I'm developing an Android app. I need to change an Element in Jsoup to a string so that I can subtract the two. The code below and String value = valueOf(arrT.val()); do not work. I'm not sure how to convert an Element into a string. My code is:

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);

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();



 }

}
}   

It gives me a compile error saying that prdt cannot change from an Element to a string. I would love any help you could give on this, because there is noting online that I can find on this. Thank you.

hichris123
  • 10,145
  • 15
  • 56
  • 70

3 Answers3

3

Use Jsoup text() function

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date date2 = sdf.parse(prdt.text());
akw
  • 46
  • 2
  • Just curious, what's the difference between the var function and the text function? Var didn't work, but this did. – hichris123 Sep 24 '13 at 00:07
1

try out:

String value = arrT.val(); SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

Date date = sdf.parse(value);

see the jsoup apidocs

Note: the date pattern must match with the format you are passing to the parse method.

Additional note: as doc = Jsoup.connect(URL) does network calls, consider putting it in an AsyncTask instead of calling directly on the UI Thread (in the onCreate() method).

Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
  • This doesn't work, I have the date part okay, but value = null and arrT = 20130922 14:15:16. – hichris123 Sep 22 '13 at 18:05
  • @hichris123 HH:mm:ss this is the format you want but actually that doesn't matter the format is in which your data is that is yyyyMMdd hh:mm:ss Check my answer again – Trikaldarshiii Sep 22 '13 at 18:10
  • The string in the TextView doesn't display anything, so I don't think that this method works. – hichris123 Sep 22 '13 at 21:55
1

If you are getting date in

Then it should be like this and are you forgetting prdt.val()

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss");
Date date = sdf.parse(prdt.val());
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
  • the Element prdt actually equals 20130922 14:39:23 Could this have an affect on why it returns nothing after `Date date2 = sdf.parse(prdt.val()); Date date1 = sdf.parse(arrT.val());`? – hichris123 Sep 22 '13 at 19:40
  • How can I strip out the and ? – hichris123 Sep 22 '13 at 20:24
  • Check for the values of String.valueOf(prdt.val()) what it is – Trikaldarshiii Sep 22 '13 at 20:28
  • It equals 20130922 16:33:17 . Will this work to strip out the tag? http://stackoverflow.com/questions/8694984/remove-part-of-string – hichris123 Sep 22 '13 at 20:33
  • use can use this string.replace("<",""); string.replace("/>",""); string.replace("prdt",""); them trim the string remember prdt changes with arrT so in that case change it – Trikaldarshiii Sep 22 '13 at 20:35
  • Problem again is that `}Elements elemn = doc.select("eta"); for (Element div : elem) { Elements arrT = div.select("arrT"); Elements prdt = div.select("prdt"); String.valueOf(prdt.val()); String value = String.valueOf(arrT.val()); tv.setText(String.valueOf (value));` doesn't display anything on the TextView. – hichris123 Sep 22 '13 at 20:41