0

我在从网站接收 PHP Echo 消息时遇到一些问题。我有这个代码:

HttpClient httpclient=new DefaultHttpClient(); 
   HttpPost httppost=new  HttpPost("http://www.itbstudios.tk/test.php");
   HttpResponse response = httpclient.execute(httppost);
   String str =  EntityUtils.toString(response.getEntity());

我在 stackoverflow 上找到了这段代码和更多内容,但它不起作用。该应用程序正在崩溃httpclient.execute();

互联网权限在 android manifest 中设置。

php代码只是一个<?php echo "test"; ?>

我正在为 API 15 构建并在 HTC 设备上进行测试。

你们能帮帮我吗?我什至创建了另一个项目并将代码粘贴到创建中,我遇到了同样的问题。

谢谢!

4

1 回答 1

3

HttpClient 在高于 10 的 API 中有一些错误。请改用 HttpUrlConnection。

GET 方法示例:

URL url = new URL("http://www.itbstudios.tk/test.php");
HttpUrlConnection mUrlConnection = (HttpURLConnection) url.openConnection();
mUrlConnection.setDoInput(true);

InputStream is = new BufferedInputStream(mUrlConnection.getInputStream());
String s = readStream(is);
于 2014-01-10T22:04:18.487 回答