【发布时间】:2018-09-22 00:01:12
【问题描述】:
这是我的 logcat,第一行是我尝试连接的 url,它是有效的 url
她是我的代码导致错误并拒绝连接
public static String makeHttpRequest(URL url) throws IOException {
String jsonResponse = "";
Log.e(LOG_TAG, url.toString());
// If the URL is null, then return early.
if (url == null) {
return jsonResponse;
}
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// If the request was successful (response code 200),
// then read the input stream and parse the response.
if (urlConnection.getResponseCode() == 200) {
inputStream = urlConnection.getInputStream();
jsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode());
}
} catch (IOException e) {
Log.e(LOG_TAG, "Problem retrieving the Category JSON results.", e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return jsonResponse;
}
我已经知道如何使 Http 连接并且以前做过很多但这是第一次在本地服务器上
【问题讨论】:
-
服务器在设备上吗?不?那你为什么使用本地主机?问了无数次...
-
是的,笔记本电脑上的服务器,我在模拟器@Selvin 上运行应用程序
-
在模拟器上 localhost 显然是模拟器而不是模拟器的主机。随意搜索类似问题的答案。
-
我是主机,我运行我的模拟器!!怎么了
-
模拟器的主机是你的电脑而不是你。同样,在模拟器 localhost 上是模拟器而不是模拟器的主机......你懂英语吗?