Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class JsonGetData extends AsyncTask<Void,Void,String> {
- private TextView texter;
- private Context context;
- private ListView listview;
- private ArrayList<Departure>departureList;
- private String ide;
- public JsonGetData(TextView textv, Context c,ListView l, String id) {
- texter = textv;
- context = c;
- listview = l;
- departureList = new ArrayList<>();
- ide = id;
- }
- @Override
- protected String doInBackground(Void... params) {
- HttpURLConnection connection = null;
- BufferedReader reader = null;
- String URL = getURL(ide);
- try {
- URL url = new URL(URL);
- connection = (HttpURLConnection) url.openConnection();
- connection.connect();
- InputStream stream = connection.getInputStream();
- reader = new BufferedReader(new InputStreamReader(stream));
- StringBuffer buffer = new StringBuffer();
- String line = "";
- while((line = reader.readLine()) != null){
- buffer.append(line);
- }
- String finalJson = buffer.toString();
- JSONObject parentObject = new JSONObject(finalJson);
- JSONArray parentArray;
- JSONObject object = parentObject.getJSONObject("DepartureBoard");
- String serverTime = object.getString("servertime");
- parentArray = object.getJSONArray("Departure");
- JSONObject oneTime = parentArray.getJSONObject(0);
- String busStop = oneTime.getString("stop");
- for (int i = 0 ; i<parentArray.length(); i++) {
- Departure departure = new Departure();
- JSONObject jRealObject = parentArray.getJSONObject(i);
- departure.setDestination(jRealObject.getString("direction"));
- departure.setTimeLeft(getTime(serverTime, jRealObject.getString("rtTime")));
- departure.setRtTime(jRealObject.getString("rtTime"));
- departure.setvNumber(jRealObject.getString("sname"));
- departure.setBgColor(jRealObject.getString("fgColor"));
- departure.setTxColor(jRealObject.getString("bgColor"));
- departure.setPosition(jRealObject.getString("track"));
- departureList.add(departure);
- }
- Collections.sort(departureList, new Comparator<Departure>() {
- @Override
- public int compare(Departure lhs, Departure rhs) {
- return lhs.getTimeLeft().compareTo(rhs.getTimeLeft());
- }
- });
- return busStop;
- }
- catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- } catch (ParseException e) {
- e.printStackTrace();
- } finally{
- if(connection != null) {
- connection.disconnect();
- }
- try {
- if(reader!=null){
- reader.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- super.onPostExecute(result);
- DepartureAdapter adapter = new DepartureAdapter(context, R.layout.custom_row_2,departureList);
- listview.setAdapter(adapter);
- texter.setText(result);
- }
- public String getTime(String serverTime, String rtTime) throws ParseException {
- SimpleDateFormat format = new SimpleDateFormat("HH:mm");
- Date srvTime = format.parse(serverTime);
- Date rTime = format.parse(rtTime);
- long difference = rTime.getTime() - srvTime.getTime();
- if(difference == 0){
- return "Nu";
- }else {
- long diffMin = difference / (60 * 1000) % 60;
- return "" + diffMin + "min";
- }
- }
- private String getURL(String id){
- String key="8ac536c3-8ba4-46b0-9f3f-dfda1d57e48d";
- String ide=id;
- String URL;
- URL ="http://api.vasttrafik.se/bin/rest.exe/v1/departureBoard?authKey="+key+"&format=json&jsonCallback=processJSON&id="+ide+"&timeSpan=8";
- return URL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement