1

I want to save this JSON in url { https://www.instagram.com/ihanan95/?__a=1 } and save it as string by java I try used all suggestions but when do it I get empty string.

1
  • Please check answers. Commented Apr 11, 2017 at 13:57

2 Answers 2

2

You can use Spring's RestTemplate to get the response as String, e.g.:

RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject("https://www.instagram.com/ihanan95/?__a=1", String.class);
System.out.println(response);

If you are not allowed to use third party libaries then you can do the same with URLConnection, e.g.:

URLConnection connection = new URL("https://www.instagram.com/ihanan95/?__a=1").openConnection();
try(Scanner scanner = new Scanner(connection.getInputStream());){
    String response = scanner.useDelimiter("\\A").next();
    System.out.println(response);
}
Sign up to request clarification or add additional context in comments.

Comments

2

Read JSON file from remote website.

Example:

URL url = new URL("https://www.instagram.com/ihanan95/?__a=1");
JSONTokener tokener = new JSONTokener(url.openStream());
JSONObject obj = new JSONObject(tokener);
JSONObject data = obj.getJSONObject("user");
String message = data.getString("message");

JSONTokener Doc

Dependencies

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20090211</version>
</dependency>

2 Comments

The constructor JSONTokener(InputStream) is undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.