Android-尝试连接到本地服务器(Xampp)时出错
问题描述:
这是我的logcat和我尝试连接的网址的第一行,它是有效的网址
here's my logcat and the first line the url i try to connect with and it's valid url
她是我的导致错误并导致连接拒绝的代码
her is my code that cause the error and make the connection to refuse
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连接了,但这是第一次在本地服务器上完成
i already know how to make Http connect and done it a lot before but this the first time on local server
答
由于模拟器和笔记本电脑的本地主机不同,因此您需要执行以下操作:
Since local host for Emulator and Laptop is different, you need to do following:
- 使用笔记本电脑的命令提示符中的
ipconfig
命令获取PC的IP地址. - 您应该在URL中使用
ipAddress
,而不是使用localhost
.
- Get the IP address of your PC using:
ipconfig
command in your Laptop's command prompt. - Instead of using
localhost
, you should useipAddress
in your URL.
因此您的地址将变为: http://< ip地址>/lotus/getstats.php?category = ATM
So your address will become:
http://<ip address>/lotus/getstats.php?category=ATM