du4629 2012-11-18 16:14
浏览 55
已采纳

从Android连接到PHP和MySQL

I want to connect to a database from my android application, and have found a tutorial to do that (link to tutorial).

It works fine when I'm using it in the emulator, but not when I try to use it on my phone. I have tried with a WAMP server where I have port forwarded it and I have tried with a real web host (000webhost). The only difference from the emulator to the phone is the URL, where I have to provide a public IP address instead of 10.0.2.2. I have also tried with the local IP of the computer hosting the WAMP server. I know that some webhosts add ads to the code, but I'm pretty sure WAMP doesn't do that.

When I'm using the phone I get the following error from LogCat:

Error parsing data org.json.JSONException: Value <html> of type java.lang.String cannot be converted to JSONObject

Update 1:

I have tried using cURL to check what's returning. It returns a lot (title, bgcolor etc.), but I think this is interesting:

Access denied for user 'root'@'localhost' (using password: NO) in C:\wamp\www\android_connect\db_connect.php on line <i>28</i></th></tr>

It seems like it can't access the database, but I might be wrong?

When i use cURL to access localhost it returns the correct items from the database and not all the html code as the server with the public IP does.

Update 2:

I solved the problem with the public WAMP server. I was a noob and forgot that I had set up the server with a password.

But when I use the webhost (000webhost) from a browser I still get this error:

PHP Error Message

Warning: require_once(__DIR__/db_connect.php) [function.require-once]: failed to open stream: No such file or directory in /home/a7471305/public_html/android_connect/get_all_products.php on line 12

Free Web Hosting

PHP Error Message

Fatal error: require_once() [function.require]: Failed opening required '__DIR__/db_connect.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/a7471305/public_html/android_connect/get_all_products.php on line 12

Free Web Hosting

And I get this error from cURL:

<b>Fatal error</b>:  require_once() [<a href='function.require'>function.require
</a>]: Failed opening required '__DIR__/db_connect.php' (include_path='.:/usr/li
b/php:/usr/local/lib/php') in <b>/home/a7471305/public_html/android_connect/get_
all_products.php</b> on line <b>12</b><br />

How can this problem be solved?

  • 写回答

4条回答 默认 最新

  • doudou348131346 2012-11-19 17:35
    关注

    I found the solution. Thanks to everyone who helped me.

    the webhost couldn't find the file, because I was using

    require_once( __DIR__ . '/db_connect.php');
    

    To find the next php file in the same folder. Instead I should have used:

    define('__ROOT__', dirname(dirname(__FILE__))); 
    require_once(__ROOT__.'/android_connect/db_connect.php'); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?