2

I have used this code http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ for GCM . But having trouble whenever i click to view or create a new produce button.I have checked the solution that is described below the tutorial but that doesn't solve my problem. here is my

02-14 20:06:09.775: W/KeyCharacterMap(738): No keyboard for id 0
02-14 20:06:09.775: W/KeyCharacterMap(738): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-14 20:06:13.105: E/JSON Parser(738): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-14 20:06:13.105: W/dalvikvm(738): threadid=9: thread exiting with uncaught exception (group=0x40015560)
02-14 20:06:13.115: E/AndroidRuntime(738): FATAL EXCEPTION: AsyncTask #1
02-14 20:06:13.115: E/AndroidRuntime(738): java.lang.RuntimeException: An error occured while executing doInBackground()
02-14 20:06:13.115: E/AndroidRuntime(738):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-14 20:06:13.115: E/AndroidRuntime(738):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-14 20:06:13.115: E/AndroidRuntime(738):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-14 20:06:13.115: E/AndroidRuntime(738):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-14 20:06:13.115: E/AndroidRuntime(738):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-14 20:06:13.115: E/AndroidRuntime(738):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-14 20:06:13.115: E/AndroidRuntime(738):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-14 20:06:13.115: E/AndroidRuntime(738):  at java.lang.Thread.run(Thread.java:1019)
02-14 20:06:13.115: E/AndroidRuntime(738): Caused by: java.lang.NullPointerException
02-14 20:06:13.115: E/AndroidRuntime(738):  at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:130)
02-14 20:06:13.115: E/AndroidRuntime(738):  at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
02-14 20:06:13.115: E/AndroidRuntime(738):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-14 20:06:13.115: E/AndroidRuntime(738):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-14 20:06:13.115: E/AndroidRuntime(738):  ... 4 more
02-14 20:06:14.213: E/WindowManager(738): Activity com.example.androidhive.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405375e0 that was originally added here
02-14 20:06:14.213: E/WindowManager(738): android.view.WindowLeaked: Activity com.example.androidhive.AllProductsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405375e0 that was originally added here
02-14 20:06:14.213: E/WindowManager(738):   at android.view.ViewRoot.<init>(ViewRoot.java:258)
02-14 20:06:14.213: E/WindowManager(738):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
02-14 20:06:14.213: E/WindowManager(738):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-14 20:06:14.213: E/WindowManager(738):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
02-14 20:06:14.213: E/WindowManager(738):   at android.app.Dialog.show(Dialog.java:241)
02-14 20:06:14.213: E/WindowManager(738):   at com.example.androidhive.AllProductsActivity$LoadAllProducts.onPreExecute(AllProductsActivity.java:117)
02-14 20:06:14.213: E/WindowManager(738):   at android.os.AsyncTask.execute(AsyncTask.java:391)
02-14 20:06:14.213: E/WindowManager(738):   at com.example.androidhive.AllProductsActivity.onCreate(AllProductsActivity.java:57)
02-14 20:06:14.213: E/WindowManager(738):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 20:06:14.213: E/WindowManager(738):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-14 20:06:14.213: E/WindowManager(738):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-14 20:06:14.213: E/WindowManager(738):   at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-14 20:06:14.213: E/WindowManager(738):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-14 20:06:14.213: E/WindowManager(738):   at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 20:06:14.213: E/WindowManager(738):   at android.os.Looper.loop(Looper.java:123)
02-14 20:06:14.213: E/WindowManager(738):   at android.app.ActivityThread.main(ActivityThread.java:3683)
02-14 20:06:14.213: E/WindowManager(738):   at java.lang.reflect.Method.invokeNative(Native Method)
02-14 20:06:14.213: E/WindowManager(738):   at java.lang.reflect.Method.invoke(Method.java:507)
02-14 20:06:14.213: E/WindowManager(738):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-14 20:06:14.213: E/WindowManager(738):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-14 20:06:14.213: E/WindowManager(738):   at dalvik.system.NativeStart.main(Native Method)
02-14 20:06:25.245: I/Process(738): Sending signal. PID: 738 SIG: 9

Here is my asynctask code ---

/**
     * 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
         * */
        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
                    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(),
                            NewProductActivity.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);
                }
            });

        }

    }

after doing the thing that ben has said ...the length of error has reduced ..... now having only this error --

02-14 20:39:06.094: E/JSON Parser(1182): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
02-14 20:39:06.094: W/dalvikvm(1182): threadid=9: thread exiting with uncaught exception (group=0x40015560)
02-14 20:39:06.104: E/AndroidRuntime(1182): FATAL EXCEPTION: AsyncTask #1
02-14 20:39:06.104: E/AndroidRuntime(1182): java.lang.RuntimeException: An error occured while executing doInBackground()
02-14 20:39:06.104: E/AndroidRuntime(1182):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at java.lang.Thread.run(Thread.java:1019)
02-14 20:39:06.104: E/AndroidRuntime(1182): Caused by: java.lang.NullPointerException
02-14 20:39:06.104: E/AndroidRuntime(1182):     at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:137)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:1)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-14 20:39:06.104: E/AndroidRuntime(1182):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-14 20:39:06.104: E/AndroidRuntime(1182):     ... 4 more

2 Answers 2

2

Here is the main error:

Caused by: java.lang.NullPointerException at 
com.example.androidhive.AllProductsActivity$LoadAllProducts.doInBackground(AllProductsActivity.java:130)

Whatever is happening at line 130 in your doInBackground method is causing a nullPointer.

Please point out which line is 130 in your code.

Sign up to request clarification or add additional context in comments.

5 Comments

I have copied the code from the site that i gave . my code is as same as this tutorial.
what line is 130? and it would help if you post the AsyncTask code in your question. It is quite a ways down on that page and tangled up with the server side stuff.
i want to add that i am able to connect to my database through server side but cannot do anything with android . and it is in my line 130 code : Log.d("All Products: ", json.toString());
@Developer json object is null. So the device isn't fetching it properly from the server. Do you have INTERNET permission in your manifest? and is the device you are testing on online at the time that you try to make the server request?
yes i have used xampp . and apache is online. and i have inserted one data in the datbase manually for testing . I have added that permission also.
0

1) Find the IP Address: Type in ipconfig in command prompt:

Find IP Address

2) Make sure it works by typing in your IP address/android_connect/get_all_products.php into you browser. For example, for me it’s “http://192.168.1.79/android_connect/get_all_products.php” If it works, u should see this:

PHP Right Message

If you’re like me, you see this: PHP Error Message

So the error message is being sent back with the JSON file and is causing a NullPointerException in the Android code.

3) If you’re getting the PHP error message, turn off the error message reporting.

To change the PHP settings so that the error message doesn’t get return, go here. From taskbar -> Right click on WAMP-> PHP-> php.ini

Php.ini

Change the text “display_errors= On” to “display_errors= Off”

PHP Settings

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.