Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. public class JsonGetData extends AsyncTask<Void,Void,String> {
  2. private TextView texter;
  3. private Context context;
  4. private ListView listview;
  5. private ArrayList<Departure>departureList;
  6. private String ide;
  7.  
  8. public JsonGetData(TextView textv, Context c,ListView l, String id) {
  9. texter = textv;
  10. context = c;
  11. listview = l;
  12. departureList = new ArrayList<>();
  13. ide = id;
  14.  
  15.  
  16. }
  17. @Override
  18. protected String doInBackground(Void... params) {
  19. HttpURLConnection connection = null;
  20. BufferedReader reader = null;
  21. String URL = getURL(ide);
  22. try {
  23. URL url = new URL(URL);
  24. connection = (HttpURLConnection) url.openConnection();
  25. connection.connect();
  26.  
  27. InputStream stream = connection.getInputStream();
  28. reader = new BufferedReader(new InputStreamReader(stream));
  29. StringBuffer buffer = new StringBuffer();
  30. String line = "";
  31. while((line = reader.readLine()) != null){
  32. buffer.append(line);
  33. }
  34. String finalJson = buffer.toString();
  35. JSONObject parentObject = new JSONObject(finalJson);
  36. JSONArray parentArray;
  37. JSONObject object = parentObject.getJSONObject("DepartureBoard");
  38. String serverTime = object.getString("servertime");
  39. parentArray = object.getJSONArray("Departure");
  40. JSONObject oneTime = parentArray.getJSONObject(0);
  41. String busStop = oneTime.getString("stop");
  42.  
  43. for (int i = 0 ; i<parentArray.length(); i++) {
  44. Departure departure = new Departure();
  45. JSONObject jRealObject = parentArray.getJSONObject(i);
  46.  
  47. departure.setDestination(jRealObject.getString("direction"));
  48. departure.setTimeLeft(getTime(serverTime, jRealObject.getString("rtTime")));
  49. departure.setRtTime(jRealObject.getString("rtTime"));
  50. departure.setvNumber(jRealObject.getString("sname"));
  51. departure.setBgColor(jRealObject.getString("fgColor"));
  52. departure.setTxColor(jRealObject.getString("bgColor"));
  53. departure.setPosition(jRealObject.getString("track"));
  54.  
  55. departureList.add(departure);
  56. }
  57. Collections.sort(departureList, new Comparator<Departure>() {
  58. @Override
  59. public int compare(Departure lhs, Departure rhs) {
  60. return lhs.getTimeLeft().compareTo(rhs.getTimeLeft());
  61. }
  62. });
  63. return busStop;
  64.  
  65.  
  66. }
  67. catch (MalformedURLException e) {
  68. e.printStackTrace();
  69. } catch (IOException e) {
  70. e.printStackTrace();
  71. } catch (JSONException e) {
  72. e.printStackTrace();
  73. } catch (ParseException e) {
  74. e.printStackTrace();
  75. } finally{
  76. if(connection != null) {
  77. connection.disconnect();
  78. }
  79. try {
  80. if(reader!=null){
  81. reader.close();
  82. }
  83. } catch (IOException e) {
  84. e.printStackTrace();
  85. }
  86.  
  87. }
  88. return null;
  89. }
  90.  
  91. @Override
  92. protected void onPostExecute(String result) {
  93. super.onPostExecute(result);
  94.  
  95. DepartureAdapter adapter = new DepartureAdapter(context, R.layout.custom_row_2,departureList);
  96. listview.setAdapter(adapter);
  97. texter.setText(result);
  98.  
  99. }
  100.  
  101.  
  102.  
  103. public String getTime(String serverTime, String rtTime) throws ParseException {
  104.  
  105. SimpleDateFormat format = new SimpleDateFormat("HH:mm");
  106. Date srvTime = format.parse(serverTime);
  107. Date rTime = format.parse(rtTime);
  108. long difference = rTime.getTime() - srvTime.getTime();
  109. if(difference == 0){
  110. return "Nu";
  111. }else {
  112. long diffMin = difference / (60 * 1000) % 60;
  113. return "" + diffMin + "min";
  114. }
  115.  
  116. }
  117.  
  118. private String getURL(String id){
  119. String key="8ac536c3-8ba4-46b0-9f3f-dfda1d57e48d";
  120. String ide=id;
  121. String URL;
  122. URL ="http://api.vasttrafik.se/bin/rest.exe/v1/departureBoard?authKey="+key+"&format=json&jsonCallback=processJSON&id="+ide+"&timeSpan=8";
  123. return URL;
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement