3

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.

RamenChef
  • 5,557
  • 11
  • 31
  • 43
appkovacs
  • 97
  • 1
  • 1
  • 9
  • The message means: "there's no server at this ip address". – Christine Nov 13 '16 at 19:39
  • @Christine : There is, because I checked the link again, which is pasted into the application. – appkovacs Nov 13 '16 at 19:48
  • It looks suspiciously like a timeout issue to me - see the "after 90000ms" component of the error message. When you say that myip is accessible via web browser, do you mean the web browser on the device on which you're running the app? – clownba0t Nov 13 '16 at 19:52
  • use devtools and the GET u mentioned in a browser to display the headers used by browser... go back to POST and use SAME headers that you dumped in browser.devTools... – Robert Rowntree Nov 13 '16 at 20:19
  • `myip is accessible via web browser`. You should have used a web browser on your Android device. Did you? – greenapps Nov 13 '16 at 21:17
  • 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. – Christine Nov 13 '16 at 22:47
  • 1
    @Christine : Thanks, that was the problem and my fault. Thanks for help again! – appkovacs Nov 14 '16 at 05:10
  • 2
    @Christine : Sorry, I have tried to load some pages, but yes it was a network problem. – appkovacs Nov 14 '16 at 13:37

2 Answers2

5

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.

Christine
  • 5,617
  • 4
  • 38
  • 61
0

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.

Jishad
  • 151
  • 4
  • 14
  • I am also facing the same problem where turning off mobile data solves the problem. But I need to know if there is a fix even with the Mobile Data turned ON. Any suggestions? – Joshua Feb 19 '21 at 08:54