0

我找到了一个可以帮助我的教程.. 这个使用 MySQL 数据库获取数据并将其显示在 ListView 控件中.. 问题是我在处理 AsyncTask 时遇到错误.. 所以这是我的个人代码和错误报告

   package pre.commande;

   import android.os.Bundle;
   import android.app.Activity;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.support.v4.app.NavUtils;
   import java.util.ArrayList;
   import java.util.HashMap;
   import java.util.List;

   import org.apache.http.NameValuePair;
   import org.json.JSONArray;
   import org.json.JSONException;
   import org.json.JSONObject;


   import android.app.ListActivity;
   import android.app.ProgressDialog;
   import android.content.Intent;
   import android.os.AsyncTask;
   import android.util.Log;
   import android.view.View;
   import android.widget.AdapterView;
   import android.widget.AdapterView.OnItemClickListener;
   import android.widget.ListAdapter;
   import android.widget.ListView;
   import android.widget.SimpleAdapter;
   import android.widget.TextView;

   public class MainActivity extends ListActivity {



// Progress Dialog
private ProgressDialog pDialog;

// Creating JSON Parser object
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> produitList;

// url to get all products list
private static String url_all_products = "http://10.0.2.2/precommande/ts_produit.php";


// JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUIT = "produit";
    private static final String TAG_ID = "id";
    private static final String TAG_DES = "des";

    // products JSONArray
    JSONArray produit = null;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

    // Hashmap for ListView
            produitList = new ArrayList<HashMap<String, String>>();

            // Loading products in Background Thread
            new LoadAllProducts().execute();

            // Get listview
            ListView lv = getListView();

   }



   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.activity_main, menu);
       return true;
   }


   /**
 * 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(MainActivity.this);
        pDialog.setMessage("Loading products. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // 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
                produit = json.getJSONArray(TAG_PRODUIT);

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

                    // Storing each json item in variable
                    String id = c.getString(TAG_ID);
                    String des = c.getString(TAG_DES); 

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, id);
                    map.put(TAG_DES, des);

                    // adding HashList to ArrayList
                    produitList.add(map);
                }
            } 
        } 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(
                        MainActivity.this, produitList,
                        R.layout.list_item, new String[] { TAG_ID,
                                TAG_DES},
                        new int[] { R.id.pid, R.id.name });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }

    }


}

错误报告

没有检测到 GPU 仿真的仿真器。08-06 11:07:06.318: E/WindowManager(1347): Activity pre.commande.MainActivity 泄露了最初添加在这里的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@b6c6afc0 08-06 11: 07:06.318: E/WindowManager(1347): android.view.WindowLeaked: Activity pre.commande.MainActivity 泄露了最初添加在这里的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@b6c6afc0 08-06 11 :07:06.318: E/WindowManager(1347): 在 android.view.ViewRoot.(ViewRoot.java:258) 08-06 11:07:06.318: E/WindowManager(1347): 在 android.view.WindowManagerImpl.addView (WindowManagerImpl.java:148) 08-06 11:07:06.318: E/WindowManager(1347): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 08-06 11:07:06.318: E/WindowManager (1347):在 android.view。

4

1 回答 1

0

在清单文件中添加以下行。

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

上面的<application>标签。

于 2016-05-08T17:44:17.297 回答