Android - error while try connect to local server(Xampp)

自古美人都是妖i 提交于 2019-12-01 13:10:44

问题


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;
}

i already know how to make Http connect and done it a lot before but this the first time on local server


回答1:


Since local host for Emulator and Laptop is different, you need to do following:

  1. Get the IP address of your PC using: ipconfig command in your Laptop's command prompt.
  2. Instead of using localhost, you should use ipAddress in your URL.

So your address will become: http://<ip address>/lotus/getstats.php?category=ATM



来源:https://stackoverflow.com/questions/49786287/android-error-while-try-connect-to-local-serverxampp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!