Android - error while try connect to local server(Xampp)(Android - 尝试连接到本地服务器时出错(Xampp))
本文介绍了Android - 尝试连接到本地服务器时出错(Xampp)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的 logcat,第一行是我尝试连接的 url,它是有效的 url
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
这篇关于Android - 尝试连接到本地服务器时出错(Xampp)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Android - 尝试连接到本地服务器时出错(Xampp)


基础教程推荐
猜你喜欢
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01