본문 바로가기
이건 알아야지's/오늘 공부

다운로드 sb로 읽기 코드

by 하루디 2021. 8. 11.

 

 

"이건 진짜 기본이야 기본 이것도 못하면 코딩한다고 말하지도 마"라고 말씀하셨다.... 선생님^,ㅜ 더 열심히 하겠습니다

 

public String download(){
try {
  URL url = new URL("http://localhost:8000/user");
  HttpURLConnection conn = (HttpURLConnection)url.openConnection();
  BufferedReader br =
  new BufferedReader(new InputStreamReader(conn.getInputStream()));
  StringBuffer sb = new StringBuffer();
  String input = null;
  
  while((input = br.readLine()) != null){
  	sb.append(input);
  }
 // sb를 json을 자바오브젝트 파싱
        return sb.toString();
 // 그림을 그리기
 } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}