通过互联网从数据库中获取数据的问题

时间:2013-11-03 09:33:45

标签: php android database

我正在学习android所以我开发了一个小应用程序,我试图通过互联网访问数据库所以这样做我首先尝试使用localhost使用WAMP服务器和PHP现在我只是修改了必要的文件,并上传到一个网站,现在当我运行应用程序时,它尝试通过互联网获取数据,然后显示以下LOGCAT。

我无法理解它的内容:

10-30 23:48:23.986: E/WindowManager(4484): Activity com.kamani.nirav.androidphpdemo.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40cd4c88 V.E..... R.....ID 0,0-384,96} that was originally added here
10-30 23:48:23.986: E/WindowManager(4484): android.view.WindowLeaked: Activity com.kamani.nirav.androidphpdemo.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40cd4c88 V.E..... R.....ID 0,0-384,96} that was originally added here
10-30 23:48:23.986: E/WindowManager(4484):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
10-30 23:48:23.986: E/WindowManager(4484):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
10-30 23:48:23.986: E/WindowManager(4484):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-30 23:48:23.986: E/WindowManager(4484):  at android.app.Dialog.show(Dialog.java:281)
10-30 23:48:23.986: E/WindowManager(4484):  at com.kamani.nirav.androidphpdemo.AllProductsActivity$LoadAllProducts.onPreExecute(AllProductsActivity.java:54)
10-30 23:48:23.986: E/WindowManager(4484):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
10-30 23:48:23.986: E/WindowManager(4484):  at android.os.AsyncTask.execute(AsyncTask.java:534)
10-30 23:48:23.986: E/WindowManager(4484):  at com.kamani.nirav.androidphpdemo.AllProductsActivity.onCreate(AllProductsActivity.java:41)
10-30 23:48:23.986: E/WindowManager(4484):  at android.app.Activity.performCreate(Activity.java:5104)
10-30 23:48:23.986: E/WindowManager(4484):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-30 23:48:23.986: E/WindowManager(4484):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-30 23:48:23.986: E/WindowManager(4484):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-30 23:48:23.986: E/WindowManager(4484):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-30 23:48:23.986: E/WindowManager(4484):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-30 23:48:23.986: E/WindowManager(4484):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 23:48:23.986: E/WindowManager(4484):  at android.os.Looper.loop(Looper.java:137)
10-30 23:48:23.986: E/WindowManager(4484):  at android.app.ActivityThread.main(ActivityThread.java:5041)
10-30 23:48:23.986: E/WindowManager(4484):  at java.lang.reflect.Method.invokeNative(Native Method)
10-30 23:48:23.986: E/WindowManager(4484):  at java.lang.reflect.Method.invoke(Method.java:511)
10-30 23:48:23.986: E/WindowManager(4484):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-30 23:48:23.986: E/WindowManager(4484):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-30 23:48:23.986: E/WindowManager(4484):  at dalvik.system.NativeStart.main(Native Method)

以下是我的应用的代码:

public class AllProductsActivity extends ListActivity {

private ProgressDialog processDialog;

JSONParser jsonParse = new JSONParser();

ArrayList<HashMap<String, String>> productsList;

private static String url_all_products = "http://www.niravkamani.com/get_all_products.php";

private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "name";

JSONArray products = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_products);

    productsList = new ArrayList<HashMap<String, String>>();

    new LoadAllProducts().execute();

}

class LoadAllProducts extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        processDialog = new ProgressDialog(AllProductsActivity.this);
        processDialog.setMessage("Loading items...Please wait...");
        processDialog.setIndeterminate(false);
        processDialog.setCancelable(false);
        processDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {

        JSONObject json = jsonParse.makeHttpRequest(url_all_products);

        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {

                products = json.getJSONArray(TAG_PRODUCTS);

                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);

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

                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);

                    productsList.add(map);

                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        processDialog.dismiss();

        ListAdapter listAdapter = new SimpleAdapter(
                AllProductsActivity.this, productsList, R.layout.list_item,
                new String[] { TAG_PID, TAG_NAME }, new int[] { R.id.pid,
                        R.id.name });

        setListAdapter(listAdapter);
    }
}

}

1 个答案:

答案 0 :(得分:1)

当您引用或保留某些上下文时,即使不再需要它,也会发生窗口泄漏。

尝试检查您的进度对话框是否仍处于活动状态&amp;如果它处于活动状态,则需要在onDestroy()生命周期方法中解除progressdialog。

例如:

  

@覆盖       protected void onDestroy(){

    super.onDestroy();
    if (pDialog != null && pDialog.isShowing()) {

        pDialog.dismiss();
    }
}

pDialog是我的progressdialog对象