0

I'm working on app that enables me to load users from php mysql and when I select on of them it opens a new activity to display the name of user and button to delete it. the problem is when I run the app, AllUsersActivity dosen't return the users. It displays an empty page here is the code of the method LoadAllUsers, there was an "else " to move to another avtivity to create user if there was not any user. but I removed it because I want it to display users and delete them without adding. After I removed the add part it returned to me null, although the connection is working right and it retrived the data from the DB in the logcat. can anyone help me with it please

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

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

        /**
         * getting All users 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_users, "GET", params);

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

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

                if (success == 1) {
                    // user found
                    // Getting Array of user
                    user = json.getJSONArray(TAG_USER);

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

                        // Storing each json item in variable
                        String U_mail = c.getString(TAG_UMAIL);
                        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_UMAIL, U_mail);
                        map.put(TAG_NAME, Name);

                        // adding HashList to ArrayList
                        usersList.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 users
            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    ListAdapter adapter = new SimpleAdapter(
                            AllUsersActivity.this, usersList,
                            R.layout.list_item, new String[] { TAG_UMAIL,
                                    TAG_NAME},
                            new int[] { R.id.um, R.id.name });
                    // updating listview
                    setListAdapter(adapter);
                }
            });

here is the logcat

04-23 09:22:36.814: W/System.err(916): org.json.JSONException: No value for users
04-23 09:22:36.814: W/System.err(916):  at org.json.JSONObject.get(JSONObject.java:354)
04-23 09:22:36.814: W/System.err(916):  at org.json.JSONObject.getJSONArray(JSONObject.java:544)
04-23 09:22:36.824: W/System.err(916):  at com.example.androidhive.AllUsersActivity$LoadAllUsers.doInBackground(AllUsersActivity.java:140)
04-23 09:22:36.824: W/System.err(916):  at com.example.androidhive.AllUsersActivity$LoadAllUsers.doInBackground(AllUsersActivity.java:1)
04-23 09:22:36.824: W/System.err(916):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-23 09:22:36.865: W/System.err(916):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)

 04-23 09:22:36.924: W/System.err(916):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    04-23 09:22:36.924: W/System.err(916):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor

.java:573)
Najd
  • 78
  • 1
  • 7
  • 1
    the exception is clear . Your json does not contain a entry with the key "users" – Blackbelt Apr 23 '13 at 09:30
  • There is only one return statement. And it's "return null". Why do you expect doInBackground to return something else ? – Gordak Apr 23 '13 at 09:31
  • check your json..your json dont have "users" key so add condition to check whether your json have "users"key or not – Prachi Apr 23 '13 at 09:32
  • @blackbelt It was working correct until I removed the add part – Najd Apr 23 '13 at 09:33
  • @Gordak I removed return null but that causes many error. what do u suggest me to do – Najd Apr 23 '13 at 09:34
  • @curious_mind sorry I don't get it can u explain more, why should I add a condition I code it to have users key sorry for my bad english – Najd Apr 23 '13 at 09:38
  • 1
    your logcat says you don't have "users" key on your json object and your tyring to fetch data from "users" keys so first check whether "user" object exists in your json or not – Prachi Apr 23 '13 at 09:46

2 Answers2

1

You don't have an onPostExecute() override, there you should be dismissing the dialog and updating your UI.

I also noticed you use return null; in your doInBackground() method, if you want to return a string as your method signature suggests then you would return a string here to pass into the onPostExecute() method.

As Gordak suggested you could change your method signature to AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> (NB you have to use a capital V for Void ) then you can simply return your HashMap and process it in onPostExecute()

EDIT

I noticed a couple of things with your updated question, first, your exception is being caused because when you try to load the users array the field isn't found in the JSON. user = json.getJSONArray(TAG_USER); Check your feed to make sure there aren't any typos in the field names, or TAG_USER.

Second you always apss null into your onPostExecute() so the file path will always be null.

Finally, onPostExecute() always executes on the UI thread so you don't need the runOnUIThread() call.

ScouseChris
  • 4,377
  • 32
  • 38
0

I don't understand what you are trying to do. I guess you what to get data from a JSON string. Looking at your code, this data contains some an array of JSONObject. Each object containing a mail and a name.

What I would probably do in this case. Instead of extending a AsyncTask<String, String, String>, extend a AsyncTask<void, void, ArrayList<HashMap<String, String>>>. This way, your doInBackground method return a ArrayList<HashMap<String, String>> which is maybe more interesting than a single String.

At the end of your for loop, return usersList.

If you get an exception, handle it and return null or a empty new ArrayList> object.

Then decide what you are going to do in the onPostExecute method.

Hope it helps you.

Gordak
  • 2,060
  • 22
  • 32
  • I did what u suggest it dosen't work. I put return usersList; instead of return null. and I changed the signature to what u seggest but it didn't work – Najd Apr 23 '13 at 10:14
  • usersList is null for two reasons. First, you don't instantiate it. Second, you get an JSONException. Can you provide us the url of your JSON String ? – Gordak Apr 23 '13 at 10:16
  • Maybe you should discuss with [Katherine](http://stackoverflow.com/questions/16165728/android-notifydatasetchanged-not-working). Your questions are very similar. I guess you are working for the same project. – Gordak Apr 23 '13 at 10:21
  • @gordac i did this in onCreate // Hashmap for ListView usersList = new ArrayList>(); // Loading users in Background Thread new LoadAllUsers().execute(); do u mean this by "instantiate it" and please check onPostExecute I update the question – Najd Apr 23 '13 at 10:23
  • @Gordac no I don't know her. I think we are following the same tutorial – Najd Apr 23 '13 at 10:25
  • Well which tutorial is it ? – Gordak Apr 23 '13 at 10:42
  • @gordac check androidhive if u r interested – Najd Apr 23 '13 at 14:19