Parse JSON from HttpURLConnection object

后端 未结 5 1870
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 11:04

I am doing basic http auth with the HttpURLConnection object in Java.

        URL urlUse = new URL(url);
        HttpURLConnection conn = null;
         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 12:00

    The JSON string will just be the body of the response you get back from the URL you have called. So add this code

    ...
    BufferedReader in = new BufferedReader(new InputStreamReader(
                                conn.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) 
        System.out.println(inputLine);
    in.close();
    

    That will allow you to see the JSON being returned to the console. The only missing piece you then have is using a JSON library to read that data and provide you with a Java representation.

    Here's an example using JSON-LIB

提交回复
热议问题