0

I'm having some trouble with the Here API. When I run the curl command I get a JSON Object, but when I'm trying it in my code I keep getting HTML back. Can anyone please help me? What am I doing wrong? This is the curl command:

 curl \
  --compressed \
  -H 'Accept-Encoding:gzip' \
  -H 'Accept-Language:en-US,en;q=0.5' \
  --get 'https://places.cit.api.here.com/places/v1/autosuggest' \
    --data-urlencode 'app_code=my_code' \
    --data-urlencode 'app_id=my_id' \
    --data-urlencode 'at=48.13642,11.57755' \
    --data-urlencode 'pretty=true' \
    --data-urlencode 'q=Hofbräuhaus am Platz' 

And this is my Java code:

private void searchForPlacesInMunich(String place) throws IOException {
        String link = "https://places.cit.api.here.com/places/v1/autosuggest"
                + "?app_id=" + app_id
                + "&app_code=" + app_code
                + "&at=" + latitude + "%2C" + longitude
                + "&q=" + place
                + "&pretty";
        URL url = new URL(link);
          HttpURLConnection con = (HttpURLConnection) url.openConnection();
          con.setRequestProperty("Accept-Encoding", "gzip");
          con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
          con.setRequestMethod("GET");
          con.connect();
          System.out.println("Length : " + con.getContentLength());

          Reader reader = null;
          if ("gzip".equals(con.getContentEncoding())) {
             reader = new InputStreamReader(new GZIPInputStream(con.getInputStream()));
          }
          else {
             reader = new InputStreamReader(con.getInputStream());
          }

          while (true) {
             int ch = reader.read();
             if (ch==-1) {
                break;
             }
             System.out.print((char)ch);
          }
    }
benny b
  • 175
  • 1
  • 12
  • Some more info because I feel like I didn't explain the problem well enough: The problem is: When I'm sending the GET-request via internet browser, it works fine (because it gets HTML as response), however in the bottom left of the page there is a curl command under "Take this request home" (the curl command is in the question). When I'm running the command in a terminal, I get the raw JSON response (no HTML), but when I'm running my Java code (also in the question) I am only getting the HTML response and not the JSON as expected. – benny b Dec 15 '18 at 15:16

2 Answers2

0

Try with JSONObject

JSONObject myObject = new JSONObject(result);

Source from: Get a JSON object from a HTTP response

AbA2L
  • 110
  • 7
  • I'm not sure if it will work though because I get a HTML response (HTML-tags and everything). I might have asked the wrong question. It's more about the Here API then it's about Java. I mean when I'm accessing the URL in my browser I get a normal site and in the bottom left corner there's a curl command that one can copy and get the response in JSON format (raw, without any HTML) and that's what I'm trying to get in my Java code – benny b Dec 15 '18 at 15:15
0

Just found a way to get JSON instead of UI: The answer was pretty simple. Just add a callback in the URL Inspired by HERE places REST apis returns web ui instead on json

benny b
  • 175
  • 1
  • 12