Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package GetData;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import org.json.JSONException;
  14.  
  15. import org.json.JSONObject;
  16.  
  17. /**
  18. *
  19. * @author Bartek
  20. */
  21. public class WasteOfTime {
  22.  
  23. /**
  24. * @param args the command line arguments
  25. */
  26. public static void main(String[] args) throws MalformedURLException, IOException, JSONException, Exception {
  27. // TODO code application logic here
  28. String line = "326";
  29. BusData bus = new BusData();
  30. try{
  31. JSONObject json = new JSONObject(readUrl("https://api.tfl.gov.uk/Line/"+line+"/Arrivals?app_id=&app_key="));
  32. bus.setDirection( (String) json.get("direction"));
  33. }catch(JSONException e){
  34. e.printStackTrace();
  35. }
  36. System.out.print(bus.getDirection());
  37. }
  38.  
  39.  
  40. private static String readUrl(String urlString) throws Exception {
  41. BufferedReader reader = null;
  42. try {
  43. URL url = new URL(urlString);
  44. reader = new BufferedReader(new InputStreamReader(url.openStream()));
  45. StringBuffer buffer = new StringBuffer();
  46. int read;
  47. char[] chars = new char[1024];
  48. while ((read = reader.read(chars)) != -1)
  49. buffer.append(chars, 0, read);
  50.  
  51. return buffer.toString();
  52. } finally {
  53. if (reader != null)
  54. reader.close();
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement