如何存储API的输出?

时间:2013-12-31 11:50:05

标签: java api url

我正在使用此代码访问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;
        }
    }

像这样我得到返回值200.这意味着我成功击中了网址。但我必须存储API的输出。我的API返回uniqueId。 如何存储输出?

3 个答案:

答案 0 :(得分:3)

试试这个:

InputStream is = conn.getInputStream();
BufferedReader br= new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output= br.readLine();
System.out.println(output);

答案 1 :(得分:2)

试试这个

URL link = new URL(urlToHit);
in = new BufferedReader(new InputStreamReader(link.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine );
}//while

答案 2 :(得分:0)

要在中获取响应,您可以使用:

InputStream isr = http.getInputStream();

call isr.read()从流中获取输出。