通过php和JSON将android应用程序连接到MySQL

时间:2014-07-02 05:22:50

标签: php android mysql json

我正在尝试在MySQL上的远程服务器上创建一个带有数据库的Android应用程序。我有一个带有查询的php文件,结果返回一行。

这是我的JSON类代码:

public class JSONClass extends AsyncTask<String,Void,String>{

    public HashMap<String, String> tbl = new HashMap<String, String>();
    private Context context;

    public JSONClass(Context context) {

        this.context = context;

    }

    @Override

    protected String doInBackground(String... arg0) {

        try{

            HttpClient client = new DefaultHttpClient();

            String link = "http://pickupfriend.fulba.com/android_project/query1.php";

            HttpPost post = new HttpPost(link);

            post.addHeader("Content-Type", "application/x-www-form-urlencoded");

            HttpResponse response = client.execute(post);

            HttpEntity entity = response.getEntity();

            String result = EntityUtils.toString(entity, "utf-8");

            JSONArray ja = new JSONArray(result);

            for(int i = 0 ; i < ja.length() ; i++){

                String str = ja.getJSONObject(i).getString("DisplayName");

                int uID = ja.getJSONObject(i).getInt("UserID"); 
            }

        }catch(Exception e){


        }

        return "";

    }

}

php文件返回一个答案,您可以尝试一下,只需在浏览器中输入“http://pickupfriend.fulba.com/android_project/query1.php”即可。答案是:

[{"UserID":"1","DisplayName":"Itzick Binder"}]

我的应用程序遇到以下行时崩溃:

HttpResponse response = client.execute(post);

这是我在android studio上的错误日志中遇到的错误:

07-02 20:38:17.444    5778-5778/com.example.pickup.app E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-02 20:38:18.335    5778-5995/com.example.pickup.app W/dalvikvm﹕ threadid=14: thread exiting with uncaught exception (group=0x41eb7ba8)
07-02 20:38:18.345    5778-5995/com.example.pickup.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #4
    Process: com.example.pickup.app, PID: 5778
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            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:841)
     Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
            at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6024)
            at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:853)
            at android.view.ViewGroup.invalidateChild(ViewGroup.java:4320)
            at android.view.View.invalidate(View.java:10878)
            at android.widget.TextView.invalidateRegion(TextView.java:4651)
            at android.widget.TextView.invalidateCursor(TextView.java:4594)
            at android.widget.TextView.spanChange(TextView.java:7502)
            at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:9214)
            at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979)
            at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688)
            at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
            at android.text.Selection.setSelection(Selection.java:76)
            at android.text.Selection.setSelection(Selection.java:87)
            at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302)
            at android.widget.TextView.setText(TextView.java:3801)
            at android.widget.TextView.setText(TextView.java:3671)
            at android.widget.EditText.setText(EditText.java:80)
            at android.widget.TextView.setText(TextView.java:3646)
            at extras.JSONClass.doInBackground(JSONClass.java:57)
            at extras.JSONClass.doInBackground(JSONClass.java:17)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            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:841)
07-02 20:38:18.355    5778-5778/com.example.pickup.app E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-02 20:38:18.355    5778-5778/com.example.pickup.app E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
07-02 20:38:20.077    5778-5995/com.example.pickup.app I/Process﹕ Sending signal. PID: 5778 SIG: 9

有人可以告诉我,我做错了什么吗?提前谢谢

1 个答案:

答案 0 :(得分:1)

如果它崩溃,您的应用可能没有相应的访问网络权限。你添加了:

    <uses-permission android:name="android.permission.INTERNET" />

到您的AndroidManifest.xml文件?

另外,一个小问题,URL在这一行似乎写得不正确:

String link = "http://pickupfriend.fulba.com/android_project.query1.php";

看起来应该是android_project/query1.php