2

I am developing an Android app. I am setting a difference of 2 dates to a TextView. It doesn't display anything. Somewhere, it isn't getting to the TextView. I would love if you could tell me where I'm going wrong. My code is below. Now after putting in new code, it force closes. Code updated with new code

TestStation.java

public class TestStation extends Activity {
String URL = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=201412abc85d49b2b83f907f9e329eaa&mapid=40380";
@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");

String value = arrT.val(); 
String valu = prdt.val();

Date date1 = new Date(valu);
Date date2 = new Date(value);

long dateDiff = (date1.getTime() - date2.getTime())>0 ? (date1.getTime() - date2.getTime()) :(date2.getTime() - date1.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:00");
sdf.format(dateDiff);
tv.setText(String.valueOf (dateDiff));

}

}

The LogCat shows lots of red (errors):

09-22 12:39:38.348: E/AndroidRuntime(9713): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dev.chicagotraintracker/com.dev.chicagotraintracker.TestStation}:     java.lang.IllegalArgumentException: Parse error: 
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.os.Looper.loop(Looper.java:137)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.lang.reflect.Method.invokeNative(Native Method)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.lang.reflect.Method.invoke(Method.java:525)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at dalvik.system.NativeStart.main(Native Method)
09-22 12:39:38.348: E/AndroidRuntime(9713): Caused by: java.lang.IllegalArgumentException: Parse error: 
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.util.Date.parseError(Date.java:367)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.util.Date.parse(Date.java:563)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at java.util.Date.<init>(Date.java:156)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at com.dev.chicagotraintracker.TestStation.onCreate(TestStation.java:53)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.Activity.performCreate(Activity.java:5133)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-22 12:39:38.348: E/AndroidRuntime(9713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

How can I solve this?

hichris123
  • 10,145
  • 15
  • 56
  • 70
  • you are doing network related operation on the ui thread. use Asynctask. also you can have 1 try block and multiple catch block – Raghunandan Sep 22 '13 at 01:50
  • do you get any exceptions? – Raghunandan Sep 22 '13 at 02:06
  • its a xml the link you posted. Why not use a xml pull parser to extract the data from tags. open the link in your browser and check it yourself. `20130921 21:12:30 20130921 21:18:30` xml tags – Raghunandan Sep 22 '13 at 02:14
  • @Raghunandan, need more information from the xml, not all in code yet. Logcat did show orange, I posted it above. – hichris123 Sep 22 '13 at 02:18
  • its not html your link provided gives you xml so parse the xml tags using xmlpullparser. for example check the docs – Raghunandan Sep 22 '13 at 07:35

1 Answers1

1

Problem is not with the TextView

The main problem is with date parsing

You do not catch the first parsing which trows an exception it may not be in correct format to be catched check both value and vlau

     try{
        Date date = sdf.parse(value);
        try
        {
            Date dat = sdf.parse(valu);
            long diff = dat.getTime() - date.getTime();
            tv.setText(String.valueOf(diff));
        }
        catch(ParseException f)
        {
            f.printStackTrace();
        }
        finally
        {}
    }
    catch(ParseException f)
    {
        f.printStackTrace();
    }
    finally
    {}

EDIT 1:

You can get the DateDifference this way

Date date1 = new SimpleDateFormat("GiveYourFormate", Locale.ENGLISH).parse(stringDate1);
Date date2 = new SimpleDateFormat("GiveYourFormate", Locale.ENGLISH).parse(stringDate2);
long dateDiff = (date1.getTime() - date2.getTime())>0 ? (date1.getTime() - date2.getTime()) :(date2.getTime() - date1.getTime());
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:00");
sdf.format(dateDiff);
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
  • You're right. I'm trying to parse only the time part of 2292013 10:53:22. I'm only wanting the 10:53:22, but I have the entire string. How can I parse only that part? @High-Tech KitKat Android – hichris123 Sep 22 '13 at 14:53
  • If you want time and you are subtracting it with some date then its not an issue coz date part canceled – Trikaldarshiii Sep 22 '13 at 15:52
  • @hichris123 check edit 1 you can do this way the date difference – Trikaldarshiii Sep 22 '13 at 16:00
  • @High-Tech KitKat Android, new errors after updating code, logcat and code updated above. – hichris123 Sep 22 '13 at 16:43
  • It's 20130922 11:48:56, but I only want the hh:mm:ss. Do I need to do yyyymmdd hh:mm:ss? – hichris123 Sep 22 '13 at 16:50
  • I think it's because element arrT isn't being succesfully converted into string value. How can I do this? @Hi-Tech KitKat Android – hichris123 Sep 22 '13 at 16:56
  • Yes its not convert able to this method – Trikaldarshiii Sep 22 '13 at 16:58
  • Is there any way to subtract the two and display it? Or do I have to use a different method to retreive and parse the XML? – hichris123 Sep 22 '13 at 17:00
  • @hichris123 check my update your answer is there it will solve the problem give ti your format it will parse it – Trikaldarshiii Sep 22 '13 at 17:05
  • this may help you too http://stackoverflow.com/questions/4216745/java-string-to-date-conversion/4216767#4216767 – Trikaldarshiii Sep 22 '13 at 17:05
  • @High-Tech KitKat Android The problem is not with the parsing, it's with the conversion of the element to a string. Is there another way to convert them? – hichris123 Sep 22 '13 at 17:32
  • @hichris123 yes String.valueOf(value) – Trikaldarshiii Sep 22 '13 at 17:34
  • How do you set that to a variable/string, though? Right now, it converts it but I can't do anything with it. @Hi-Tech KitKat Android – hichris123 Sep 22 '13 at 17:38
  • No, it's just the problem of Jsoup. The elements don't seem to want to be converted. That way doesn't work either. It still shows prdt and arrT as Elements, not variables or strings. – hichris123 Sep 22 '13 at 17:47
  • @hichris123 are you too **Nobe** or what can't you do this *String.valueOf(prdt.val())* what's the problem it will do any how even when it is Grid or null; what is val()'s return type; you said you are getting value in particular format; if you really have then you will get it string format using this **Do you have problem in solution or Solution in Problems** – Trikaldarshiii Sep 22 '13 at 17:48
  • @hichris123 so do proper homework with Jsoup Element and if it changes to String use the method i posted your problem will be solved – Trikaldarshiii Sep 22 '13 at 17:50
  • I don't think that there's any information on that, though. That's why I asked it here. The method I was using was String value = arrT.val(); String valu = prdt.val();, and someone posted that, but it didn't work. – hichris123 Sep 22 '13 at 17:51
  • why don't you do this String value = String.valueOf(arrT.val()); don't you think it will it think it must – Trikaldarshiii Sep 22 '13 at 17:53
  • I try that, but it sets it value equal to null. However, arrT shows up as the date and time. – hichris123 Sep 22 '13 at 17:59
  • there must be some way to get that and ask a separate question regarding this problem this problem is concern with date time which i posted now take care of you jsoup object – Trikaldarshiii Sep 22 '13 at 18:01
  • Okay. Thanks for your help. Question is here http://stackoverflow.com/questions/18938568/convert-jsoup-element-to-string. – hichris123 Sep 22 '13 at 18:05