Android - 尝试连接到本地服务器时出错(Xampp)

时间:2018-04-12 00:39:28

标签: android xampp

这是我的logcat和我尝试连接的网址的第一行以及它的有效网址

here's my logcat and the first line the url i try to connect with and it's valid 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连接并且之前已经完成了很多但是这是第一次在本地服务器上

1 个答案:

答案 0 :(得分:0)

由于Emulator和Laptop的本地主机不同,您需要执行以下操作:

  1. 使用笔记本电脑命令提示符中的ipconfig命令获取PC的IP地址。
  2. 您应该在网址中使用localhost,而不是使用ipAddress
  3. 所以你的地址将成为: http://<ip address>/lotus/getstats.php?category=ATM