I am trying to call an api and display the result using the HttpURLConnection library.
However this is the result I am getting.
(HTTPLog)-Static: isSBSettingEnabled false
(HTTPLog)-Static: isSBSettingEnabled false
connection established
The code keeps running forever and no result is shown as the contents of the String ans should be printed
The rest Api I'm trying to call: https://jsonplaceholder.typicode.com/users
Interestingly enough if I add a "System.out.println(readLine);" in the while loop; it prints the entire json twice as it should.
Please help
package com.example.TestingApi;
import java.io.*;
import java.net.*;
public class TestingApi {
public static void main(String args[]){
try{
String ans="";
URL url=new URL("https://jsonplaceholder.typicode.com/users/");
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.connect();
int c=conn.getResponseCode();
if(c==HttpURLConnection.HTTP_OK){
System.out.println("connection established");
BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while(true){
String readLine=in.readLine();
ans=ans+readLine;
if(readLine==null)
break;
}
System.out.println(ans);
}
else
System.out.println("Connection failed");
}
catch(Exception ex){
}
}
}