Android ECONNREFUSED (Connection refused)

appkovacs picture appkovacs · Nov 13, 2016 · Viewed 8.7k times · Source

I have tried to send data to server with this code from my android application.

try {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("http://myip/adminlogin.php");
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    HttpResponse response = httpClient.execute(httpPost);

    HttpEntity entity = response.getEntity();

    is = entity.getContent();

    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();

    String line = null;
    while ((line = reader.readLine()) != null)
    {
        sb.append(line + "\n");
    }
    result = sb.toString();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return result;

I got following error message when try to login with username and password pair.

[socket][0] connection /myip:80;LocalPort=33049(0) close [socket][/0.0.0.0:33049] W/System.err: org.apache.http.conn.HttpHostConnectException: Connection to http://myip refused Caused by: java.net.ConnectException: failed to connect to /myip (port 80) after 90000ms: isConnected failed: ECONNREFUSED (Connection refused)

myip is accessible via web browser, so I think port 80 is OK. File path is also OK. I checked the databse and it is running properly. I set necessary permissions in Manifest file.

Answer

Christine picture Christine · Nov 14, 2016

Is your phone on the same network as the computer where you connect with the ip number? Is the server on the public internet? I get this error when I forget to put my phone on my local wifi network and it wants to connect to a server on my local network.