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);