I'm trying to make an application that will parse the page with the bus schedule and display it.
The bottom line is that the schedule was taken first stop and if the bus is not earlier than the current time. That is, if it is 18:10, then it is necessary to output the remaining schedule from 18:10.
http://mybuses.ru/moscow/bus/734/
Ideally, there would only be a display of 3 buses that are closest to the current time.
class main extends AsyncTask<Void, Void, Void>{ String Bus1,Sys_time,wtf; @Override protected Void doInBackground(Void... params) { String table=""; String time; Integer first,second; final Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(System.currentTimeMillis()); Date date = cal.getTime(); Integer mHour = date.getHours(); Integer mMinute = date.getMinutes(); if(mMinute<10){ mMinute=date.getMinutes()+10; } String curr_time=mHour.toString()+":"+mMinute.toString(); Document doc; try { doc = Jsoup.connect("http://mybuses.ru/moscow/bus/734/").get(); Element table_site=doc.select("table").get(0); Element rows1=table_site.select("tr").get(1); Iterator<Element> rowIterator=table_site.select("tr").iterator(); rowIterator.next(); Integer str1,str2,str3,str4; while (rowIterator.hasNext()) { Element e = doc.select("tr").get(2); String e_1=e.attr("class"); str1=e_1.indexOf("-"); str2=e_1.indexOf(":"); str3=e_1.indexOf("-"); str4 = e_1.indexOf(":"); first = Integer.parseInt(e_1.substring(str1+1, e_1.indexOf(str1+2))); second = Integer.parseInt(e_1.substring(str2+1, e_1.indexOf(str2+2))); if (first>=mHour && second>=mMinute) { Element rows=table_site.select("tr").get(1); Element record1=rows.select("td").get(1); Iterator<Element> time1=record1.select("td").iterator(); wtf = time1.next().text(); } else { wtf="1232132132"; } } Element record2=rows1.select("td").get(6); Iterator<Element> time2=record2.select("td").iterator(); table="Автобус прибудет на Ст. Солнечная в "+wtf+" (через n минут)\nАвтобус прибудет на ГМС3 в "+time2.next().text(); } catch (IOException e) { e.printStackTrace(); } Bus1=table; Sys_time=curr_time; return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); textView.setText("Текущее время: "+Sys_time+"\n\nРасписание автобусов: \n"+Bus1); } }
This code has an error:
Caused by: java.lang.StringIndexOutOfBoundsException: length = 41; regionStart = 23; regionLength = -24
in line
first = Integer.parseInt(e_1.substring(str1+1, e_1.indexOf(str1+2)));
How can I fix it? My idea was to get time from the class name on the site, because the class name indicates the time of the first stop of sh2 columnIsVisible 0-05:26-01-0002-0001
, where 05:26
is the time. I tried to cut this time and compare it with the current one.