将MySQL数据库连接到Android应用程序

时间:2016-01-12 18:11:57

标签: java android mysql json

这类似于 app crashes on JSON jparser make http request

制作应用程序后,我用

替换了URL行
private static String url_all_products = "http://192.168.0.101:80/android_connect/get_all_products.php";

当我在浏览器上运行上面的url时,我收到一条消息,说已经删除了一个方法,然后是一个JSON对象(我手动插入一行)

然而,当我在我的设备上运行应用程序时,按下查看产品按钮后它会崩溃

    01-12 23:39:10.508 3681-3789/com.example.androidhive E/JSON Parser: Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 01-12 23:39:10.510 3681-3789/com.example.androidhive E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #5
Process: com.example.androidhive, PID: 3681
java.lang.RuntimeException: An error occured while executing doInBackground()
   at android.os.AsyncTask$3.done(AsyncTask.java:304)
   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
   at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
   at java.util.concurrent.FutureTask.run(FutureTask.java:242)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
   at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:130)
   at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:105)
   at android.os.AsyncTask$2.call(AsyncTask.java:292)
   at java.util.concurrent.FutureTask.run(FutureTask.java:237)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
   at java.lang.Thread.run(Thread.java:818)  01-12 23:39:10.510 3681-3681/com.example.androidhive D/AbstractTracker: Event success 01-12 23:39:10.511 3681-3789/com.example.androidhive E/AndroidRuntime: Error reporting crash
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
   at android.os.Handler.<init>(Handler.java:200)
   at android.os.Handler.<init>(Handler.java:114)
   at android.content.AsyncQueryHandler.<init>(AsyncQueryHandler.java:125)
   at net.oneplus.odm.insight.tracker.AbstractTracker$TrackerAsyncQueryHandler.<init>(AbstractTracker.java:108)
   at net.oneplus.odm.insight.tracker.AbstractTracker.<init>(AbstractTracker.java:60)
   at net.oneplus.odm.insight.tracker.AppTracker.<init>(AppTracker.java:24)
   at 

    com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:97)
       at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
       at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690) 01-12 23:39:10.511 3681-3789/com.example.androidhive I/Process: Sending signal. PID: 3681 SIG: 9

NullPointerException让我相信没有返回JSON对象,但是当我在我的电脑上运行url时,我可以看到一个JSON对象。

1 个答案:

答案 0 :(得分:1)

您正在使用已弃用的PHP代码,并在您的浏览器页面内生成一个表格,表示已弃用,并且当您使用表格解析该页面时,表格为#34;意味着你不仅得到了json,还得到了html表的内容,并且无法解析为json,这就是为什么你得到一个例外。我建议遵循这个w3schools tutorial关于如何使用非弃用代码连接到mysql。