考虑以下代码。由于我正在执行 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);