【问题标题】:Android - error while try connect to local server(Xampp)Android - 尝试连接到本地服务器时出错(Xampp)
【发布时间】:2018-09-22 00:01:12
【问题描述】:

这是我的 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 连接并且以前做过很多但这是第一次在本地服务器上

【问题讨论】:

  • 服务器在设备上吗?不?那你为什么使用本地主机?问了无数次...
  • 是的,笔记本电脑上的服务器,我在模拟器@Selvin 上运行应用程序
  • 在模拟器上 localhost 显然是模拟器而不是模拟器的主机。随意搜索类似问题的答案。
  • 我是主机,我运行我的模拟器!!怎么了
  • 模拟器的主机是你的电脑而不是你。同样,在模拟器 localhost 上是模拟器而不是模拟器的主机......你懂英语吗?

标签: android xampp


【解决方案1】:

由于模拟器和笔记本电脑的本地主机不同,您需要执行以下操作:

  1. 使用笔记本电脑命令提示符中的ipconfig 命令获取 PC 的 IP 地址。
  2. 您应该在 URL 中使用 ipAddress,而不是使用 localhost

所以您的地址将变为: http://<ip address>/lotus/getstats.php?category=ATM

【讨论】:

    猜你喜欢
    • 2017-03-22
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2023-03-03
    • 2016-12-03
    • 2020-01-07
    • 2020-07-11
    相关资源
    最近更新 更多