2014-07-02 96 views
1

我試圖在MySQL上的遠程服務器上創建一個帶有數據庫的android應用程序。我有一個查詢返回一行作爲結果的PHP文件。通過PHP和JSON將Android應用程序連接到MySQL

這是我的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工作室的錯誤:

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 

有人可以告訴我我做錯了什麼嗎?預先感謝您

+3

崩潰與哪個錯誤? –

回答

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

相關問題