Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package GetData;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.json.JSONException;
- import org.json.JSONObject;
- /**
- *
- * @author Bartek
- */
- public class WasteOfTime {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws MalformedURLException, IOException, JSONException, Exception {
- // TODO code application logic here
- String line = "326";
- BusData bus = new BusData();
- try{
- JSONObject json = new JSONObject(readUrl("https://api.tfl.gov.uk/Line/"+line+"/Arrivals?app_id=&app_key="));
- bus.setDirection( (String) json.get("direction"));
- }catch(JSONException e){
- e.printStackTrace();
- }
- System.out.print(bus.getDirection());
- }
- private static String readUrl(String urlString) throws Exception {
- BufferedReader reader = null;
- try {
- URL url = new URL(urlString);
- reader = new BufferedReader(new InputStreamReader(url.openStream()));
- StringBuffer buffer = new StringBuffer();
- int read;
- char[] chars = new char[1024];
- while ((read = reader.read(chars)) != -1)
- buffer.append(chars, 0, read);
- return buffer.toString();
- } finally {
- if (reader != null)
- reader.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement