本文介紹了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)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!