2017-08-19 36 views
0

我目前正在嘗試一個教程,以連接一個Android應用程序與服務器(鏈接:https://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/)。 但是,我有一個AsyncTask應該加載數據庫的元素的困難。它會導致應用程序崩潰並且不會在logcat中留下錯誤日誌(也嘗試過「無過濾器」)。我還用try catch塊包圍了執行調用,但該應用程序仍然崩潰。任何想法是什麼導致錯誤?Android:AsyncTask導致應用程序崩潰,沒有錯誤日誌打印

這裏有從代碼提取物:

public class AllProductsActivity extends ListActivity { 
private static String url_all_products = "https://api.androidhive.info/android_connect/get_all_products.php"; 
... 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
try { 
      new LoadAllProducts().execute(); 
     }catch (Exception e){ 
      e.printStackTrace(); 
      System.out.println(e.getMessage()); 
     } 

... 

/** 
    * Background Async Task to Load all product by making HTTP Request 
    * */ 
    class LoadAllProducts extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(AllProductsActivity.this); 
      pDialog.setMessage("Loading products. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * getting All products from url 
     * */ 
     @Override 
     protected String doInBackground(String... args) { 
      // Building Parameters 
      List<Pair> params = new ArrayList<Pair>(); 
      // getting JSON string from URL 
      JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params); 

      // Check your log cat for JSON reponse 
      Log.d("All Products: ", json.toString()); 

      try { 
       // Checking for SUCCESS TAG 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // products found 
        // Getting Array of Products 
        products = json.getJSONArray(TAG_PRODUCTS); 

        // looping through All Products 
        for (int i = 0; i < products.length(); i++) { 
         JSONObject c = products.getJSONObject(i); 

         // Storing each json item in variable 
         String id = c.getString(TAG_PID); 
         String name = c.getString(TAG_NAME); 

         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 
         map.put(TAG_PID, id); 
         map.put(TAG_NAME, name); 

         // adding HashList to ArrayList 
         productsList.add(map); 
        } 
       } else { 
        // no products found 
        // Launch Add New product Activity 
        Intent i = new Intent(getApplicationContext(), 
          AddNewProductActivity.class); 
        // Closing all previous activities 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting all products 
      pDialog.dismiss(); 
      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        /** 
        * Updating parsed JSON data into ListView 
        * */ 
        ListAdapter adapter = new SimpleAdapter(
          AllProductsActivity.this, productsList, 
          R.layout.list_item, new String[] { TAG_PID, 
          TAG_NAME}, 
          new int[] { R.id.pid, R.id.name }); 
        // updating listview 
        setListAdapter(adapter); 
       } 
      }); 

     } 

    } 
+0

告訴我一件事我可以在你的代碼中看到//檢查你的日誌貓的JSON響應 Log.d(「All Products:」,json.toString()); 請告訴我你可以在日誌中看到反對json.toString()代碼? –

回答

1

問題不在於你的代碼實現。我可以在您的代碼中看到webservice網址爲

private static String url_all_products = "https://api.androidhive.info/android_connect/get_all_products.php"; 

問題在於此Web服務。這不再有效。我在測試web服務的時候提到了郵遞員教程的鏈接,這是不再工作。希望你明白我的觀點。隨意問我,如果你有任何困惑。

+0

我已經這麼認爲,這個「api」不是真正的交易x)。但爲什麼沒有拋出異常或打印日誌?此外,您是如何測試這項服務的,是否足以安裝xamp並在瀏覽器中輸入網址? – Doflaminhgo

+0

是啊,這是你問的有效問題。請檢查您是否錯誤地在日誌貓中應用了任何過濾器,或者檢查您的設備是否已連接並在Android監視器中可見。 –

+0

在這種情況下,我仍然應該在try catch塊周圍發現一些異常,不應該嗎?但我會嘗試讓我的手在服務器上,所以我可以用一個可用的網絡服務器測試代碼。我感謝你的回答:) – Doflaminhgo

相關問題