Java android与PHP的连接

Java android与PHP的连接,java,php,android,Java,Php,Android,这是我的代码: HttpClient httpclient = new DefaultHttpClient(); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("message",

这是我的代码:

HttpClient httpclient = new DefaultHttpClient();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
HttpPost httppost = new HttpPost("http://localhost/select.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
HttpClient-HttpClient=newdefaulthttpclient();
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“消息”,msg));
HttpPost HttpPost=新的HttpPost(“http://localhost/select.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);

当我启动应用程序时,应该打开select.php文件,但什么也没有发生。

确保您可以访问浏览器(手机或模拟器)中的url。如果本地主机是您的pc,请将其更改为10.0.0.2。

您要做的是从Android设备加载PHP脚本

您需要更改托管脚本的计算机的IP地址(例如)

测试时,您需要确保与android设备连接到同一网络

部署应用程序时,需要将地址更改为该脚本的活动地址,例如

我希望这会有所帮助,这应该会起作用

HttpParams httpParams=new BasicHttpParams();
HttpClient httpclient = new DefaultHttpClient(httpParams);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", msg));
HttpPost httppost = new HttpPost("http://10.0.2.2/select.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
HttpParams HttpParams=new BasicHttpParams();
HttpClient HttpClient=新的默认HttpClient(httpParams);
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“消息”,msg));
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2/select.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);

欢迎来到StackOverflow。你应该读一读-因为你的问题不清楚-1。。。这个问题被问了很多次。第一个
localhost
是仿真器/设备不是仿真器的主机。。。在UI线程上执行http操作时,ICS(甚至HCB)应用程序上的第二个抛出错误…关于localhost。。。关于异常当我在浏览器中访问url时,php脚本工作。我将“localhost”更改为“10.0.0.2”,但当我启动应用程序时,等待2-3分钟,并显示“活动登录(应用程序登录中)没有响应”。浏览器中的“”也不起作用。-1应该是10.0.2.2而不是10.0.0.2。。。有关参考资料,请参见我在问题下的评论