问题
On executing the following statement the app does not respond for a minute and then following logcat message occurs :
HttpPost httpPost = new HttpPost("http://10.0.2.2:8080/webapp/CreateUser");
logcat -
org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused
PS - i've tried using "ip address" , 127.0.0.1 AND localhost instead of 10.0.2.2 still the same case.
Also the permissions for INTERNET and ACCESS_NETWORK_STATE are also in the manifest file.
回答1:
Your server code mostly will be accessible on the local and Wifi network if you have turned on sharing and network discovery on your system.
For the physical device to see this server it has to be on the same network. Also you need to use the IP address of your server.
回答2:
Okay , the problem has been solved .
As answered by both Codemon and Jayesh , in order to use real device to run the app from Android Studio , you have to use the ip address of the server in place of 10.0.2.2 and make sure your phone device and server are in the same network:-
HttpPost httpPost = new HttpPost("http://192.168.1.53:8080/webapp/CreateUser");
I guess my internet connection was not stable , so ip was changing and thats why it didn't work before.
Thanks for your help guys.
来源:https://stackoverflow.com/questions/30111131/httphostconnectexception-10-0-2-28080-refused