简体   繁体   English

在Android应用中,与Web服务的连接失败

[英]Connection to web services fails in Android app

I am writing a test app to test a suite of JSON services. 我正在编写一个测试应用程序来测试一套JSON服务。 For starters I am testing with a simple web service that is hosted on my local machine using MAMP. 对于初学者,我正在使用一个简单的Web服务进行测试,该服务使用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 与http:// localhost:8888的连接失败

The web service has been tested and works in other applications Web服务已经过测试,可以在其他应用程序中使用

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. 将localhost更改为您的计算机的IP地址。 In this context localhost refers to the phone device (self = locahost) 在这种情况下,localhost指的是电话设备(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 如果您需要帮助,请发布您的ReadJsonTask代码

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) 还要尽量确保测试模拟器没有尝试通过网络访问你的WS(因为他不知道2网络之间的差异)

Try to access your WS with your local IP adress (like 192.168...) 尝试使用本地IP地址访问您的WS(如192.168 ......)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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