1

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);
mattboy
  • 2,870
  • 5
  • 26
  • 40
  • 1
    what kind of problems are you running into? – dldnh Mar 11 '12 at 16:08
  • The "HttpResponse response = httpclient.execute(httppost);" line in the java code fails. I'll try to edit the question to make it clearer. – mattboy Mar 11 '12 at 16:10
  • Why HttpPost when you should be using HttpGet? – Jens Mar 11 '12 at 16:20
  • Hmm, because that's what all the examples I've seen have used :). Nevertheless I tried switching to HttpGet (see updated question) but still get the same thing. – mattboy Mar 11 '12 at 16:28
  • 1
    Alrighty - so what's the stack trace of the exception you're getting? Or is it a HTTP error returned in the HttpResponse? – Jens Mar 11 '12 at 16:37
  • I'm a bit of a newb when it comes to debugging in Eclipse, but the log returns "android.os.NetworkOnMainThreadException" as Exception. Is there a better way I can debug this? Thanks. – mattboy Mar 11 '12 at 16:41

1 Answers1

1

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.

Community
  • 1
  • 1
kitti
  • 14,663
  • 31
  • 49
  • *facepalm* That was the problem. It's annoying because I actually already knew about this and had solved it once before. Thanks for reminding me. An alternative (but sloppy?) solution to starting a new thread by the way is to change the policy with the following code. `StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);` – mattboy Mar 11 '12 at 17:07