I am hitting API with this code-
我用这个代码打击API。
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 ?
像这样,我得到的回报是200。这意味着我成功地击中了url。但是我必须存储API的输出。我的API返回uniqueId。如何存储输出?
3 个解决方案
#1
3
Try this:
试试这个:
InputStream is = conn.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output= br.readLine();
System.out.println(output);
#2
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
#3
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.
并调用isr.read()以获得流的输出。
#1
3
Try this:
试试这个:
InputStream is = conn.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output= br.readLine();
System.out.println(output);
#2
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
#3
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.
并调用isr.read()以获得流的输出。