Android ECONNREFUSED (Connection refused)

2020-02-07 03:45发布

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.

2条回答
Luminary・发光体
2楼-- · 2020-02-07 04:43

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.

查看更多
Melony?
3楼-- · 2020-02-07 04:51

Adding to Christine's answer:

I had a similar situation. I was trying to connect to another android device through it's hotspot. Even though they were connected in to the same network, I encountered this problem of "connect failed: ECONNREFUSED (Connection refused)".

After spoiling a couple of days on this, I realized that my client device was connected to mobile Internet also.That means, my mobile data was ON.

I turned it OFF and Viola !

Make sure you are not connected to any other networks.

Posted this in case it may help anybody.

查看更多
登录 后发表回答