【发布时间】:2019-02-07 08:37:13
【问题描述】:
我正在使用 Usgs API 创建一个 HttpUrlConnection。这是我的网址:
经过彻底调试,似乎connection.connect连接失败,jsonResponse为空。
public static String makeHttprequest(URL url) throws IOException {
String jsonResponse = "";
HttpURLConnection connection = null;
InputStream stream = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(1000000);
connection.setConnectTimeout(1500000);
connection.connect();
stream = connection.getInputStream();
jsonResponse = readfromstream(stream);
} catch (IOException e) {
Log.e("IOException", "Error while making request");
}
return jsonResponse;
}
【问题讨论】:
-
使用 Retrofit 进行联网。不要使用 HttpURLConnection,因为你更容易出错
-
该错误表示您在解析 JSON 时遇到问题。也许 JSON 格式不正确。
-
你能发布更多来自 Earthquake_loader 的相关代码吗?网址也应该是this
-
它给出了关于 jason 解析的错误,因为 HttpUrlConnection 给出了空输出。
-
无论如何代码没有问题,但模拟器没有连接互联网。