44288

Convert Jsoup element to string

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:

<strong>TestStation.java</strong>

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.

Answer1:

Use Jsoup text() function

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); Date date2 = sdf.parse(prdt.text());

Answer2:

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 <strong>parse</strong> method.

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

Answer3:

If you are getting date in

Then it should be like this and are you forgetting <strong>prdt.val()</strong>

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hh:mm:ss"); Date date = sdf.parse(prdt.val());

Recommend

  • how to show tweets in list view in android?
  • Yelp API Android Integration [duplicate]
  • How to send dynamic data from one activity to another in android
  • FileProvider: Installing APK. There was an error parsing the package.
  • How to configure the remote discovery with Spring Security SAML?
  • WebView Twitter login window on UI thread
  • Get Next Jsoup Element with Same Name Android
  • Spring Security Login issue
  • Android - How to use Javascript in an Android WebView?
  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • Accessing Windows Azure Queues from client side javascript/jquery
  • Linux over commit heuristic
  • UIPickerView without IB?
  • Cassandra NoClassDefFoundError: com/google/common/util/concurrent/AsyncFunction
  • SqlCommand back up Database
  • WPF version of .ScaleControl?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • onBackPressed() not being executed
  • iOS: Detect app start via notification press
  • how to adjust image in a panel in Java swing?
  • How to access EntityManager inside Entity class in EJB3
  • how to do an event when i swipe from fragment to the other
  • JFileChooser in front of fullscreen Swing application
  • HTML download movie download link
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • sending/ receiving email in Java
  • Eraser for UIBezierPath
  • Convert array of 8 bytes to signed long in C++
  • align graphs with different xlab
  • Suggestions to manage Login/Logout transitions
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize