2

I am hitting API with this code-

 public static int hitUrl(String urlToHit)
    {
       try
        {
            URL url = new URL(urlToHit);
            HttpURLConnection http = (HttpURLConnection)url.openConnection();
            int statusCode = http.getResponseCode();
            return statusCode;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }

Like this I am getting return value 200. It means I am hitting url successfully. But I have to store output of API. My API returns uniqueId. How can I store the output ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sidd
  • 111
  • 1
  • 2
  • 8

3 Answers3

3

Try this:

InputStream is = conn.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output= br.readLine();
System.out.println(output);
Prateek
  • 6,785
  • 2
  • 24
  • 37
2

try this

URL link = new URL(urlToHit);
in = new BufferedReader(new InputStreamReader(link.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine );
}//while
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
0

To get the response in a stream you can use:

InputStream isr = http.getInputStream();

and call isr.read() to get the output from stream.

Kevin Wright
  • 49,540
  • 9
  • 105
  • 155
Rahul
  • 3,479
  • 3
  • 16
  • 28