Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.alten.altenTrip.service;
- import com.alten.altenTrip.model.SediAlten;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- import lombok.extern.slf4j.Slf4j;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.core.env.Environment;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- import javax.net.ssl.HttpsURLConnection;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- import java.util.Scanner;
- @Slf4j
- @Service
- public class BingWebSearch {
- @Autowired
- private Environment env;
- @Autowired
- private TokenScheduled tokenScheduled;
- // Enter a valid subscription key.
- String subscriptionKey = "ArK13lvT4io6mG71EZr9PSDG6A8DMYLxWV160SJ4dCiH83iqP_LYVFiT_Gx9a6Bv";
- String host = "http://dev.virtualearth.net/REST/v1/Routes/";
- String waypoint = "&waypoint=";
- String path = "LocalInsights?maxTime=5&timeUnit=minute&type=HotelsAndMotels";
- String key = "&key=ArK13lvT4io6mG71EZr9PSDG6A8DMYLxWV160SJ4dCiH83iqP_LYVFiT_Gx9a6Bv";
- public String SearchWeb (SediAlten office) throws Exception {
- //URL url2 = new URL(host+path+"&waypoint="+office.getLatitude()+","+office.getLongitude()+key);
- 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");
- //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");
- log.debug(String.valueOf(url2));
- // Open the connection.
- HttpsURLConnection con = (HttpsURLConnection)url2.openConnection();
- //connection.setRequestProperty("Authorization","Bearer "+tokenScheduled.getToken());
- // Receive the JSON response body.
- //InputStream stream = connection.getInputStream();
- try {
- con.setDoOutput(true);
- con.setRequestMethod("GET");
- con.setRequestProperty("User-Agent", "Java client");
- con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- StringBuilder content;
- try (var br = new BufferedReader(
- new InputStreamReader(con.getInputStream()))) {
- String line;
- content = new StringBuilder();
- while ((line = br.readLine()) != null) {
- content.append(line);
- content.append(System.lineSeparator());
- }
- }
- //String response = new Scanner(stream).useDelimiter("\\A").next();
- JSONObject jsonObj = new JSONObject(content.toString());
- String toManage;
- toManage = jsonObj.getJSONArray("features").getJSONObject(0).getJSONObject("geometry").getJSONArray("coordinates").toString();
- toManage = toManage.replace("[","");
- toManage = toManage.replace("]","");
- String[] parts = toManage.split(",");
- String longitude = parts[0];
- String latitude = parts[1];
- System.out.println("lat: "+latitude+" long: "+longitude);
- return content.toString();
- }
- finally {
- con.disconnect();
- }
- }}
- /*public static String prettify(String json_text) {
- JsonParser parser = new JsonParser();
- JsonObject json = parser.parse(json_text).getAsJsonObject();
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- return gson.toJson(json);
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement