简体   繁体   中英

Connecting android application to MySQL through php and JSON

I'm trying to create an android application with data base on a remote server on MySQL. I have a php file with a query that returns one row as result.

This is my JSON class code:

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

    }

}

The php file returns one answer, you can try it, just type " http://pickupfriend.fulba.com/android_project/query1.php " in your browser. The answer is this:

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

My application crashes when it gets to the row with:

HttpResponse response = client.execute(post);

This is the errors I'm getting on the error log on the 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

Can someone please tell me what am I doing wrong? Thank you in advance

If it's crashing there, it's likely that your app doesn't have the appropriate permission to access the network. Did you add:

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

to your AndroidManifest.xml file?

Also, a minor point, the URL seems to be written incorrectly in this line:

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

Looks like it should be android_project/query1.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM