我无法在 Android 中发出 http 请求,代码如下;权限在清单中
public class WebRequests {
public String getContent(String http) throws IOException {
HttpURLConnection connection = null;
String line = null;
try {
connection = (HttpURLConnection) new URL(http).openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setConnectTimeout(250);
connection.setReadTimeout(250);
connection.connect();
StringBuilder sb = new StringBuilder();
if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = in.readLine()) != null) {
sb.append(line);
}
Log.d("WebReqeust", line);
} else {
Log.e("WebRequest", connection.getResponseCode() + ", " + connection.getResponseMessage());
}
} catch (Throwable cause) {
cause.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return line;
}
}
这是天气查询代码
加上清单中的规定