将数据从 Android 发送到 PHP 页面
我现在正在尝试将数据从 Android 应用程序(带有模拟器)发送到 Web 服务器(一个 php 页面)。首先,我用模拟器尝试了这个程序并且它可以工作。之后,我用电话尝试了这个程序,出现了异常:
--> IO 异常:操作超时。
我的代码的一部分:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:90/takeDatas.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("enlem", latitude ));
nameValuePairs.add(new BasicNameValuePair("boylam", longitude ));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
Toast.makeText(ReportLocationActivity.this, "Client protokol exception ", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(ReportLocationActivity.this, "IO exception "+e.getMessage(), Toast.LENGTH_LONG).show();
}
我希望你能帮助我。 提前致谢。
I am trying sending data from android application (with emulator) to web server (a php page) nowadays. Firstly, I had tried this program with emulator and it was working. After that, I tried this program with telephone and an exception occurred :
--> IO Exception : The operation timed out.
One part of my code :
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:90/takeDatas.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("enlem", latitude ));
nameValuePairs.add(new BasicNameValuePair("boylam", longitude ));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
Toast.makeText(ReportLocationActivity.this, "Client protokol exception ", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(ReportLocationActivity.this, "IO exception "+e.getMessage(), Toast.LENGTH_LONG).show();
}
I hope you will help me.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
模拟器中的 localhost 是模拟器本身。不确定Windows环境,但在Linux下我可以通过IP
10.0.2.2
从模拟器访问主机系统上的Web服务器(注意:这个IP不是我的Linux的IP系统,但从模拟器中可以通过该 IP 准确访问)。您可以在此处了解有关模拟器网络的更多信息。
The
localhost
in emulator is the emulator itself. Not sure about Windows environment, but under Linux I was able to get an access to web server on the host system from the emulator by IP10.0.2.2
(Note: this IP is not the IP of my Linux system, but from the emulator it's accessible exactly by this IP).You can read more about emulator networking here.
是的,您必须使用 http://10.0.2.2/simpleSending.php。
Yes instead of using 127.0.0.1 or http://localhost:90 , you have to use http://10.0.2.2/simpleSending.php.
由于异步方法调用可能会生成此错误。因此您需要将该异步方法包装在类中。
这是代码:
this error might get generated because of asynchronous method call.So you need to wrap that Asynchronous method in a class.
Here is the code: