Android通过PHP文件连接到数据库E / Buffer错误:转换结果时出错java.lang.NullPointerException:lock == null

时间:2015-05-07 10:42:53

标签: java php android json

我正在尝试开发一个博物馆搜索应用程序并尝试在listView中列出我的php JSON结果但我无法从Android访问我的JSON结果,我将提交我的代码和我得到的错误消息:

DisplaySearchResultsActivity.java

package com.cloudlionheart.museumsearchapplication;

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.os.Bundle;
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 DisplaySearchResultsActivity extends ListActivity {
    // Progress Dialog
    private ProgressDialog pDialog;

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

    ArrayList<HashMap<String, String>> museumItemsList;

    // url to get all products list
    private static String url_search_results = "http://192.168.56.1/android_connect/get_all_products.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MUSEUM_ITEMS = "museum_items";
    private static final String TAG_MUSEUM_ITEM_ID = "museum_item_id";
    private static final String TAG_MUSEUM_ITEM_NAME = "museum_item_name";
    private static final String TAG_MUSEUM_ITEM_ARTIST = "museum_item_artist";
    private static final String TAG_MUSEUM_ITEM_HISTORICAL_PERIOD = "museum_item_historic_period";
    private static final String TAG_MUSEUM_ITEM_LOCATION = "museum_item_location";
    private static final String TAG_MUSEUM_ITEM_IMAGE = "museum_list_image";

    // products JSONArray
    JSONArray museumItems = null;

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

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

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

        // Get listview
        ListView lv = getListView();
    }

    /**
     * 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(DisplaySearchResultsActivity.this);
            pDialog.setMessage("Loading Museum Items. 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_search_results, "GET", params);

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

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

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

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

                        // Storing each json item in variable

                        String item_id = c.getString(TAG_MUSEUM_ITEM_ID);
                        String item_name = c.getString(TAG_MUSEUM_ITEM_NAME);
                        String item_artist = c.getString(TAG_MUSEUM_ITEM_ARTIST);
                        String item_historic_period = c.getString(TAG_MUSEUM_ITEM_HISTORICAL_PERIOD);
                        String item_location = c.getString(TAG_MUSEUM_ITEM_LOCATION);
                        String list_image = c.getString(TAG_MUSEUM_ITEM_IMAGE);

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

                        // adding each child node to HashMap key => value
                        map.put(TAG_MUSEUM_ITEM_ID, item_id);
                        map.put(TAG_MUSEUM_ITEM_NAME, item_name);
                        map.put(TAG_MUSEUM_ITEM_ARTIST, item_artist);
                        map.put(TAG_MUSEUM_ITEM_HISTORICAL_PERIOD, item_historic_period);
                        map.put(TAG_MUSEUM_ITEM_LOCATION, item_location);
                        map.put(TAG_MUSEUM_ITEM_IMAGE, list_image);

                        // adding HashList to ArrayList
                        museumItemsList.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(
                            DisplaySearchResultsActivity.this, museumItemsList,
                            R.layout.list_item, new String[]{TAG_MUSEUM_ITEM_ID,
                            TAG_MUSEUM_ITEM_NAME, TAG_MUSEUM_ITEM_ARTIST,
                            TAG_MUSEUM_ITEM_HISTORICAL_PERIOD, TAG_MUSEUM_ITEM_LOCATION,
                            TAG_MUSEUM_ITEM_IMAGE},
                            new int[]{R.id.museum_item_id, R.id.museum_item_name,
                                    R.id.museum_item_artist, R.id.museum_item_historic_period,
                                    R.id.museum_item_location, R.id.museum_list_image});
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }

    }
}

JSONParser.java

package com.cloudlionheart.museumsearchapplication;

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.os.Bundle;
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 DisplaySearchResultsActivity extends ListActivity {
    // Progress Dialog
    private ProgressDialog pDialog;

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

    ArrayList<HashMap<String, String>> museumItemsList;

    // url to get all products list
    private static String url_search_results = "http://192.168.56.1/android_connect/get_all_products.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MUSEUM_ITEMS = "museum_items";
    private static final String TAG_MUSEUM_ITEM_ID = "museum_item_id";
    private static final String TAG_MUSEUM_ITEM_NAME = "museum_item_name";
    private static final String TAG_MUSEUM_ITEM_ARTIST = "museum_item_artist";
    private static final String TAG_MUSEUM_ITEM_HISTORICAL_PERIOD = "museum_item_historic_period";
    private static final String TAG_MUSEUM_ITEM_LOCATION = "museum_item_location";
    private static final String TAG_MUSEUM_ITEM_IMAGE = "museum_list_image";

    // products JSONArray
    JSONArray museumItems = null;

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

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

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

        // Get listview
        ListView lv = getListView();
    }

    /**
     * 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(DisplaySearchResultsActivity.this);
            pDialog.setMessage("Loading Museum Items. 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_search_results, "GET", params);

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

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

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

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

                        // Storing each json item in variable

                        String item_id = c.getString(TAG_MUSEUM_ITEM_ID);
                        String item_name = c.getString(TAG_MUSEUM_ITEM_NAME);
                        String item_artist = c.getString(TAG_MUSEUM_ITEM_ARTIST);
                        String item_historic_period = c.getString(TAG_MUSEUM_ITEM_HISTORICAL_PERIOD);
                        String item_location = c.getString(TAG_MUSEUM_ITEM_LOCATION);
                        String list_image = c.getString(TAG_MUSEUM_ITEM_IMAGE);

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

                        // adding each child node to HashMap key => value
                        map.put(TAG_MUSEUM_ITEM_ID, item_id);
                        map.put(TAG_MUSEUM_ITEM_NAME, item_name);
                        map.put(TAG_MUSEUM_ITEM_ARTIST, item_artist);
                        map.put(TAG_MUSEUM_ITEM_HISTORICAL_PERIOD, item_historic_period);
                        map.put(TAG_MUSEUM_ITEM_LOCATION, item_location);
                        map.put(TAG_MUSEUM_ITEM_IMAGE, list_image);

                        // adding HashList to ArrayList
                        museumItemsList.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(
                            DisplaySearchResultsActivity.this, museumItemsList,
                            R.layout.list_item, new String[]{TAG_MUSEUM_ITEM_ID,
                            TAG_MUSEUM_ITEM_NAME, TAG_MUSEUM_ITEM_ARTIST,
                            TAG_MUSEUM_ITEM_HISTORICAL_PERIOD, TAG_MUSEUM_ITEM_LOCATION,
                            TAG_MUSEUM_ITEM_IMAGE},
                            new int[]{R.id.museum_item_id, R.id.museum_item_name,
                                    R.id.museum_item_artist, R.id.museum_item_historic_period,
                                    R.id.museum_item_location, R.id.museum_list_image});
                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }

    }
}

我收到以下错误。

05-07 10:27:07.177    1942-1962/com.cloudlionheart.museumsearchapplication E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
05-07 10:27:07.177    1942-1962/com.cloudlionheart.museumsearchapplication E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
05-07 10:27:07.181    1942-1962/com.cloudlionheart.museumsearchapplication E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at com.cloudlionheart.museumsearchapplication.DisplaySearchResultsActivity$LoadAllProducts.doInBackground(DisplaySearchResultsActivity.java:94)
            at com.cloudlionheart.museumsearchapplication.DisplaySearchResultsActivity$LoadAllProducts.doInBackground(DisplaySearchResultsActivity.java:69)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:856)

经过几个小时的搜索和尝试后,我仍然找不到让它工作的方法

0 个答案:

没有答案