donglu6805
donglu6805
2012-03-11 16:03
浏览 49

MySQL,Android和PHP的问题

I can find tons of examples where you can query a MySQL database from Android with name value pairs like in this example, and I can get that to work just fine.

Now, however, I want to make it even simpler and that's when I run into problems. I want to simply return an entire table with the query below.

<?php
mysql_connect("localhost","username","pw");
mysql_select_db("db");

$q=mysql_query("SELECT field FROM table");

while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();

?>

So no $_REQUEST lines are being used which is the main difference. The PHP code works fine by itself when I run it in the web browser.

The problem arises in the Android code below. It fails when running the final httpclient.execute line.

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://site.com/db.php");
//httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);

The error returned is android.os.NetworkOnMainThreadException.

I don't know if the setEntity part here is needed since I have no actual nameValuePairs that needs to be received by the PHP script? I have tried both commenting it out as above and setting some dummy value pairs, it won't connect either way. I suspect the setEntity part is where the problem is. How would I set the entity in this case? Or is it a different problem? Thanks.

UPDATE: I tried changing HttpPost to HttpGet as below, but still get the same problem.

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://site.com/db.php");
//httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httpget);

图片转代码服务由CSDN问答提供 功能建议

我可以找到大量的例子,您可以从Android查询具有名称值对的MySQL数据库,如这个例子,我可以让它工作得很好。

然而,现在,我想让它变得更简单,那就是我遇到问题的时候。 我想简单地使用下面的查询返回整个表。

 &lt;?php 
mysql_connect(“localhost”,“username”,“pw”); 
mysql_select_db(  “db”); 
 
 $ q = mysql_query(“SELECT field FROM table”); 
 
而($ e = mysql_fetch_assoc($ q))
 $ output [] = $ e; 
 
print  (json_encode($ output)); 
 
mysql_close(); 
 
?&gt; 
   
 
 

所以没有使用$ _REQUEST行,这是 主要区别。 当我在Web浏览器中运行它时,PHP代码本身可以正常工作。

问题出现在下面的Android代码中。 运行最终的httpclient.execute行时失败。

  HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(“http://site.com/db.php”); 
 // httppost。  setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse response = httpclient.execute(httppost); 
   
 
 

返回的错误是android.os.NetworkOnMainThreadException。

我不知道这里是否需要setEntity部分,因为我没有需要PHP脚本接收的实际nameValuePairs? 我已经尝试过如上所述评论并设置一些虚拟值对,它不会以任何方式连接。 我怀疑setEntity部分是问题所在。 在这种情况下我如何设置实体? 或者这是一个不同的问题? 谢谢。

更新: 我尝试将HttpPost更改为HttpGet,如下所示,但仍然遇到同样的问题。

  HttpClient httpclient = new DefaultHttpClient(); 
HttpGet httpget = new HttpGet(“http://site.com/db.php”); 
 // httpget。  setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
HttpResponse response = httpclient.execute(httpget); 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyanfeng0563
    dongyanfeng0563 2012-03-11 16:58
    已采纳

    According to this question: How to fix android.os.NetworkOnMainThreadException?

    That exception is thrown when you perform networking operations on the main thread, which is bad practice because it hurts responsiveness of your app. You should use a background thread to do the work.

    点赞 评论

相关推荐