这是我的 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 连接并且以前做过很多但这是第一次在本地服务器上
由于模拟器和笔记本电脑的本地主机不同,您需要执行以下操作:
ipconfig
笔记本电脑命令提示符中的命令获取 PC 的 IP 地址。localhost
您应该ipAddress
在 URL中使用而不是使用。所以你的地址会变成: http://<ip address>/lotus/getstats.php?category=ATM
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句