-1

Hi I am able to connect my android application to localhost but not with webserver. I am trying to call a php script to get data from server in json format, when I am trying the url address on browser ,script is working fine and same script is also working fine on localhost, but urlconnection timeout while connecting to webserver. Please find the code below of connector(where connection timeout is occuring) i also give the code of my downloader(just in case) which is extending async task and this method downloaddata is called by doinbackground. I have already tried changing read and connectiontimeout I am using godaddy for hosting. Please help I am unable to find solution anywhere. here is the image of the code and log.

Image of connector

enter image description here

Image of downloader enter image description here

Image of logs while connecting to webserver enter image description here

  • 1
    Never post _pictures_ code, post code itself. Your error message says `Connection Refused` which means the server process is not listening. Also, `localhost` always refers to the device itself that you are running the application on, if you are connecting to a server somewhere else, you need that IP address. – Matt Clark May 07 '17 at 15:25
  • Apologies, I dont have code with me right now, I got images from my friend so i posted it. What do you mean by server process is not listening – Akhil Saraswat May 07 '17 at 15:34
  • You have a `connect` method that takes an address, this is the _client_ side of a client / server connection. The server side of this connection, the destination of that address, is not listening for incoming connections. What are you trying to connect to? Who calls this method with what parameters? – Matt Clark May 07 '17 at 15:36
  • I am trying to connect to webserver server(provided by go daddy) , the downloaddata is called in doinbackground and both are inside downloader class(which extends async task) this downloader is called inside onstart of mainactivity while connector as you can see is called by downloaddata method – Akhil Saraswat May 07 '17 at 15:44

1 Answers1

0

Make sure your server is listening on an interface reachable remotely. If you bind the listener to the loopback interface (localhost or 127.0.0.1) it will only work locally.

Try binding the interface to your public IP.

If you are not on the same network, be aware that if you are behind NAT you might not be able to host a server unless you forward ports in your router or you place your machine in the router DMZ.

In addition, if you have a firewall, make sure the server application or the port used by it is whitelisted.

arboreal84
  • 2,086
  • 18
  • 21
  • Please help me understanding what do you mean by "server is listening on an interface reachable remotely". – Akhil Saraswat May 07 '17 at 15:28
  • `127.0.0.1` or `localhost` cannot be accessed remotely (e.g: from another device, even on the same networks). You can either bind to `0.0.0.0` (all interfaces), or an specific interface given by an IP address. You can see a list of interfaces using `ifconfig` on Mac OS / Linux, or `ipconfig /all` on Windows. Also make sure the port numbers match. – arboreal84 May 07 '17 at 15:29
  • this error is occuring when i m using webserver's address (my script is inside filemanager of godaddy) on localhost it's working fine – Akhil Saraswat May 07 '17 at 15:47
  • Can you do `telnet ip port` and get it to connect? If not there is a connectivity problem in there. – arboreal84 May 07 '17 at 15:51
  • yes i m able to telnet ip port i had tried both address:port and ip:port it's not responding in app – Akhil Saraswat May 07 '17 at 16:33