简体   繁体   中英

Connection to web services fails in Android app

I am writing a test app to test a suite of JSON services. For starters I am testing with a simple web service that is hosted on my local machine using MAMP. I have a button for executing the service call and a text field to display results. When I click the button in the emulator I get the following error:

Connection to http ://localhost:8888 failed

The web service has been tested and works in other applications

Here is the button click handler:

public void btnGetJsonData(View v){

    EditText txtUserInfo = (EditText) findViewById(R.id.txtUserInfo);
    new ReadJsonTask().execute( public void btnGetJsonData(View v){
    EditText txtUserInfo = (EditText) findViewById(R.id.txtUserInfo);
    new ReadJsonTask().execute("http://localhost:8888/services/_login.php?un=bobd&pw=bobby&ref=cons");
});
}

Any ideas of why this is happening?

Thanks!

Change localhost to the IP address of your computer. In this context localhost refers to the phone device (self = locahost)

You can use 10.0.2.2 to access your actual machine, it is an alias set up to help in development.

Please post your ReadJsonTask code if you want help

Also try to be sure the testing emulator isn't trying to access your WS through the net (since he doesn't know how the difference between the 2 network)

Try to access your WS with your local IP adress (like 192.168...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM