Advertisement
nicb

Untitled

Jun 29th, 2020
1,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. package com.alten.altenTrip.service;
  2. import com.alten.altenTrip.model.SediAlten;
  3. import com.google.gson.Gson;
  4. import com.google.gson.GsonBuilder;
  5. import com.google.gson.JsonObject;
  6. import com.google.gson.JsonParser;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.json.JSONArray;
  9. import org.json.JSONObject;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.core.env.Environment;
  12. import org.springframework.scheduling.annotation.Scheduled;
  13. import org.springframework.stereotype.Service;
  14. import javax.net.ssl.HttpsURLConnection;
  15. import java.io.BufferedReader;
  16. import java.io.DataOutputStream;
  17. import java.io.InputStream;
  18. import java.io.InputStreamReader;
  19. import java.net.HttpURLConnection;
  20. import java.net.URL;
  21. import java.nio.charset.StandardCharsets;
  22. import java.util.Scanner;
  23.  
  24. @Slf4j
  25. @Service
  26. public class BingWebSearch {
  27.     @Autowired
  28.     private Environment env;
  29.     @Autowired
  30.             private TokenScheduled tokenScheduled;
  31.  
  32.     // Enter a valid subscription key.
  33.     String subscriptionKey = "ArK13lvT4io6mG71EZr9PSDG6A8DMYLxWV160SJ4dCiH83iqP_LYVFiT_Gx9a6Bv";
  34.     String host = "http://dev.virtualearth.net/REST/v1/Routes/";
  35.     String waypoint = "&waypoint=";
  36.     String path = "LocalInsights?maxTime=5&timeUnit=minute&type=HotelsAndMotels";
  37.     String key = "&key=ArK13lvT4io6mG71EZr9PSDG6A8DMYLxWV160SJ4dCiH83iqP_LYVFiT_Gx9a6Bv";
  38.  
  39.  
  40.     public String SearchWeb (SediAlten office) throws Exception {
  41.  
  42.         //URL url2 = new URL(host+path+"&waypoint="+office.getLatitude()+","+office.getLongitude()+key);
  43.         URL url2 = new URL("https://api.opencagedata.com/geocode/v1/geojson?key=a576846fdc5b43b49e8f30bad4ffc735&q=Via+Nizza,+262,Lingotto+Center+Torino%2C+Italy&pretty=1&abbrv=1&limit=1");
  44.         //URL url2 = new URL("https://test.api.amadeus.com/v2/shopping/hotel-offers?cityCode=NAP&roomQuantity=1&adults=2&radius=3&radiusUnit=KM&paymentPolicy=NONE&includeClosed=false&bestRateOnly=true&view=FULL&sort=NONE");
  45.         log.debug(String.valueOf(url2));
  46.         // Open the connection.
  47.         HttpsURLConnection con = (HttpsURLConnection)url2.openConnection();
  48.         //connection.setRequestProperty("Authorization","Bearer "+tokenScheduled.getToken());
  49.         // Receive the JSON response body.
  50.         //InputStream stream = connection.getInputStream();
  51.  
  52.         try {
  53.             con.setDoOutput(true);
  54.             con.setRequestMethod("GET");
  55.             con.setRequestProperty("User-Agent", "Java client");
  56.             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  57.         StringBuilder content;
  58.  
  59.         try (var br = new BufferedReader(
  60.                 new InputStreamReader(con.getInputStream()))) {
  61.  
  62.             String line;
  63.             content = new StringBuilder();
  64.  
  65.             while ((line = br.readLine()) != null) {
  66.                 content.append(line);
  67.                 content.append(System.lineSeparator());
  68.             }
  69.         }
  70.         //String response = new Scanner(stream).useDelimiter("\\A").next();
  71.  
  72.         JSONObject jsonObj = new JSONObject(content.toString());
  73.         String toManage;
  74.         toManage = jsonObj.getJSONArray("features").getJSONObject(0).getJSONObject("geometry").getJSONArray("coordinates").toString();
  75.         toManage = toManage.replace("[","");
  76.         toManage = toManage.replace("]","");
  77.         String[] parts = toManage.split(",");
  78.         String longitude = parts[0];
  79.         String latitude = parts[1];
  80.  
  81.  
  82.         System.out.println("lat: "+latitude+" long: "+longitude);
  83.         return content.toString();
  84.     }
  85.         finally {
  86.  
  87.             con.disconnect();
  88.         }
  89.     }}
  90.  
  91.    /*public static String prettify(String json_text) {
  92.         JsonParser parser = new JsonParser();
  93.         JsonObject json = parser.parse(json_text).getAsJsonObject();
  94.         Gson gson = new GsonBuilder().setPrettyPrinting().create();
  95.         return gson.toJson(json);
  96.     }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement