Package externaldata.realtimeapi

Source Code of externaldata.realtimeapi.RealTimeTrainHelper

package externaldata.realtimeapi;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.ProtocolException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class RealTimeTrainHelper {

  private static String api_url_base = "http://api.transilien.com/gare/";
  private static String api_url_suffix = "/depart/";
 
  private static String login = "upmc104";
  private static String password = "Xp597yAs";
 
 
  public static RealTimeBundleData getDataFromAPI(String stationUIC) {

    // URL pour acc�der � l'API
    URL url = null;
   
    try {
      url = new URL(api_url_base + stationUIC + api_url_suffix);
    } catch (MalformedURLException e) {
      e.printStackTrace();
    }
   
    if (url != null) {
     
      // Authentification pour l'API
      Authenticator.setDefault (new Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication (login, password.toCharArray());
          }
      });
     
      // Etablissement de la connexion � l'API
      HttpURLConnection conn = null;
     
      try {
        conn = (HttpURLConnection) url.openConnection();
      } catch (IOException e) {
        e.printStackTrace();
      }
     
      if (conn != null) {
       
        try {
          conn.setRequestMethod("GET");
          conn.setRequestProperty("User-Agent", "Mozilla/5.0");
        } catch (ProtocolException e) {
          e.printStackTrace();
        }
       
        // Lecture de la r�ponse retourn�e par l'API
        int responseCode = 0;
       
        try {
          responseCode = conn.getResponseCode();
        } catch (IOException e) {
          e.printStackTrace();
        }
       
        if (responseCode == 200) {
         
          BufferedReader bReader = null;
          StringBuilder xmlResult = new StringBuilder();
         
          RealTimeBundleData bundleData = new RealTimeBundleData();
          bundleData.time = (new SimpleDateFormat("HH:mm")).format(new Date(conn.getDate()));
         
          try {
            bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
           
            String inputLine;
           
            while ((inputLine = bReader.readLine()) != null) {
              xmlResult.append(inputLine);
            }
          } catch (IOException e) {
            e.printStackTrace();
          } finally {

            if (bReader != null) {
              try {
                bReader.close();
              } catch (IOException e) {
                e.printStackTrace();
              }
            }
          }
         
          try {
            bundleData.datas = RealTimeTrainHelper.parseStationData(xmlResult.toString());
          } catch (Exception e) {
            e.printStackTrace();
          }
         
          return bundleData;
        }
      }
    }
   
    return null;
  }
 
 
  private static List<RealTrainData> parseStationData(String xmlToParse) throws Exception {
   
    if (xmlToParse != null) {
     
      try {
        // Cr�ation d'un SAX parser standard
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();

        // Utilisation du handler personnalis�
        RealTrainDataHandler handler = new RealTrainDataHandler();
       
        // Cr�ation d'un Stream contenant le XML � parser
        InputStream xmlStream = new ByteArrayInputStream(xmlToParse.getBytes());
       
        // Parsing
        parser.parse(xmlStream, handler);
       
        // On retourne le r�sultat du parsing
        return handler.getTrainList();
   
      } catch (ParserConfigurationException e) {
        System.out.println("Erreur de configuration du parseur");
        System.out.println("Lors de l'appel � newSAXParser()");
      } catch (SAXException e) {
        System.out.println("Erreur de parsing");
        System.out.println("Lors de l'appel � parse()");
      } catch (IOException e) {
        System.out.println("Erreur d'entr�e/sortie");
        System.out.println("Lors de l'appel � parse()");
      }
    }
   
    return null;
  }
}
TOP

Related Classes of externaldata.realtimeapi.RealTimeTrainHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.