openConnection()은 네트워크 커넥션을 만들어주지 않는다!

링크: 안드로이드개발자 홈페이지 openConnection()메서드는 메서드 이름만을 봤을때, 네트워크 접속을 가능하게 해주는 메서드처럼 보인다. 하지만 이 메서드는 실제 네트워크 커넥션을 만들어주는 메서드가 아니다. 단지, http URL connection 인스턴스( URLConnection )를 만들어 리턴시켜주는 역할만을 한다. openConnection()메서드는네트워크 접속과 관련된 매개변수, 속성 등(url, 요청메서드 등)을 조작하게 해주고, 실제 접속은 connect()메서드가 실행되는 시점에서 이뤄지게된다. /** * This method returns the entire result from the HTTP response. * * @param url The URL to fetch the HTTP response from. * @return The contents of the HTTP response. * @throws IOException Related to network and stream reading */ public static String getResponseFromHttpUrl (URL url) throws IOException { HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection() ; try { InputStream in = urlConnection.getInputStream() ; Scanner scanner = new Scanner(in) ; scanner.useDelimiter( " \\ A" ) ; boolean hasInput = scanner.hasNext() ; if (hasInput) { return scanner.next() ; ...