Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package conector;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.Scanner;
- import java.util.concurrent.TimeoutException;
- import org.json.JSONException;
- import com.rabbitmq.client.BuiltinExchangeType;
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- public class Conector {
- private final static String NOMBRE_EXCHANGE = "ajuntament";
- public static void main(String[] args) throws IOException, JSONException, TimeoutException {
- String userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";
- String authorization = "Basic aW5mb3JtYXR1cHY6dHlBMGJxVWU=";
- Scanner ent = new Scanner(System.in);
- String url;
- URL objUrl;
- HttpURLConnection connection;
- BufferedReader in;
- StringBuffer response;
- ConnectionFactory factory;
- Connection connectionF;
- Channel channel;
- String mensaje = "";
- while (!mensaje.equals("salir")) {
- System.out.println("�Qu� datos deseas buscar (trafico o bicis)?");
- mensaje = ent.nextLine();
- if (mensaje.equals("trafico")) {
- // GET
- url = "http://mapas.valencia.es/lanzadera/opendata/Tra-estado-trafico/JSON";
- // Creamos URL
- objUrl = new URL(url);
- // Creamos conexion HTTP
- connection = (HttpURLConnection) objUrl.openConnection();
- // Definimos operacion HTTP
- connection.setRequestMethod("GET");
- // A�adimos encabezados
- connection.setRequestProperty("authorization", authorization);
- connection.setRequestProperty("user-agent", userAgent);
- // Procesamos la respuesta
- in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String inputLine;
- response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- String trafico = response.toString();
- //JSONArray coleccionT = new JSONArray(trafico);
- // Conectarse con RabbitMQ
- factory = new ConnectionFactory();
- factory.setHost("localhost");
- connectionF = factory.newConnection();
- // Crear un canal de comunicaci�n
- channel = connectionF.createChannel();
- // Declaramos un Exchange de tipo Topic
- channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
- /*
- for (int i = 0; i < coleccionT.length(); i++) {
- JSONObject traf = coleccionT.getJSONObject(i);
- channel.basicPublish(NOMBRE_EXCHANGE, trafico, null, traf.toString().getBytes());
- }*/
- channel.basicPublish(NOMBRE_EXCHANGE, mensaje, null, trafico.getBytes());
- // Cerrar canal y conexion, si no se quiere enviar mas
- channel.close();
- connectionF.close();
- } else {
- // GET
- url = "http://gobiernoabierto.valencia.es/va/resource/?ds=estacionesvalenbisi&id=cb45daa3-9e97-4873-a2e5-4cd3138f8cd2";
- // Creamos URL
- objUrl = new URL(url);
- // Creamos conexion HTTP
- connection = (HttpURLConnection) objUrl.openConnection();
- // Definimos operacion HTTP
- connection.setRequestMethod("GET");
- // A�adimos encabezados
- connection.setRequestProperty("authorization", authorization);
- connection.setRequestProperty("user-agent", userAgent);
- // Procesamos la respuesta
- in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
- String inputLine;
- response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- String bicis = response.toString();
- // Conectarse con RabbitMQ
- factory = new ConnectionFactory();
- factory.setHost("localhost");
- connectionF = factory.newConnection();
- // Crear un canal de comunicaci�n
- channel = connectionF.createChannel();
- // Declaramos un Exchange de tipo Topic
- channel.exchangeDeclare(NOMBRE_EXCHANGE, BuiltinExchangeType.TOPIC);
- channel.basicPublish(NOMBRE_EXCHANGE, mensaje, null, bicis.getBytes());
- // Cerrar canal y conexion, si no se quiere enviar mas
- channel.close();
- connectionF.close();
- }
- }
- ent.close();
- }
- }
Add Comment
Please, Sign In to add comment