【问题标题】:Get PHP Echo in Android在 Android 中获取 PHP Echo
【发布时间】:2014-01-30 01:34:18
【问题描述】:

我在从网站接收 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 中设置了 Internet 权限。

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

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

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

谢谢!

【问题讨论】:

  • 您可以发布您在 LogCat 中看到的错误吗?
  • 当我尝试转到itbstudios.tk/test.php 时,它不会加载并超时。如果服务器首先工作,它会有所帮助。
  • 可能是 NetworkOnMainThreadException...
  • 另一个错误是 NetworkOnMainThreadException

标签: php android androidhttpclient


【解决方案1】:

HttpClient 在 API 高于 10 时出现了一些错误。请改用 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);

【讨论】:

  • 不使用 readstrem(is),而是使用 s = IOUtils.toString(is);
  • @Panache 引用您未包含的非标准类与引用未包含的自定义方法没有什么不同。
猜你喜欢
  • 2019-08-10
  • 1970-01-01
  • 1970-01-01
  • 2019-05-26
  • 2015-07-24
  • 1970-01-01
  • 2015-11-30
  • 2014-12-06
  • 1970-01-01
相关资源
最近更新 更多