次のコードを考えてみましょう。私はurl.openConnection()を行っているので、connection.connectは冗長ですか?はいの場合、なぜ .connect() メソッドがあるのでしょうか? 接続を閉じた後に再接続するためですか?
URL url;
url = new URL(
"http://api.longurl.org/v2/expand?format=json&title=1&user-agent=TwitterProject&url="
+ someURL);
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line = null;
String full = "";
while ((line = in.readLine()) != null) {
full = full + line;
}
jsonresponse = JSONObject.fromObject(full);