user2108957
user2108957

Reputation:

My aSyncTask is not working

my asynctask is not working. The dialog wont dismiss and the list is not being updated (i think because onPostExecute is not being called).I googled it, but without result.

package com.example.whs;

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.Menu;
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;

public class Albums extends ListActivity {
    // Progress Dialog
    private ProgressDialog pDialog;

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

    // Array for the list
    ArrayList<HashMap<String, String>> itemList;

    // url to get the items
    private static String url_all_products = "<my url here>";

    // JSON variables
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_ITEMS = "items";
    private static final String TAG_NAME = "name";
    private static final String TAG_ID = "id";
    private static final String TAG_USERNAME = "username";

    //JSON array
    JSONArray items = null;

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

        // HashMap for the items
        itemList = new ArrayList<HashMap<String, String>>();

        // Loading the items on the background
        new LoadAllItems().execute();

        // Get list-view
        ListView lv = getListView();

        // On item click
        lv.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                //do
            }
        });
    }

    // class for loading all items
    class LoadAllItems extends AsyncTask<String, String, String> {
        // Before 
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            pDialog = new ProgressDialog(Albums.this);
            pDialog.setMessage("Albums laden...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        // Get the products     
        @Override
        protected String doInBackground(String... params) {
            // Build Parameters
            List<NameValuePair> params1 = new ArrayList<NameValuePair>();
            //Get the JSON string
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params1);

            // Check for response
            Log.d("All Items: ", json.toString());

            try {
                // check for success tag
                int success = json.getInt(TAG_SUCCESS);

                if(success == 1) {
                    // found the items
                    items = json.getJSONArray(TAG_ITEMS);

                    // loop through the items
                    for (int i = 0; items.length() > i; i++){
                        // Get the item in variable c
                        JSONObject c = items.getJSONObject(i);

                        // Store in a variable
                        String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NAME);
                        String username = c.getString(TAG_USERNAME);
                        if(username == ""){
                            username = "onbekend";
                        }

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

                        // add it
                        map.put(TAG_ID, id);
                        map.put(TAG_USERNAME, username);
                        map.put(TAG_NAME, name);

                        // to arraylist
                        itemList.add(map);
                    }
                }else{
                    // nothing found
                    Intent i = new Intent(getApplicationContext(), Index.class);
                    // close the previous activities                    
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e){
                e.printStackTrace();
            }
            return null;
        }       
    }

    protected void onPostExecute(String result){
        // dismiss dialog
        pDialog.dismiss();      
        runOnUiThread(new Runnable(){
            public void run(){
                // Add adapter to the list
                MenuAdapter adapter = new MenuAdapter(Albums.this, itemList);
                ListView list = (ListView)findViewById(R.id.list);
                list.setAdapter(adapter);   
                }
            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.albums, menu);
        return true;
    }

}

the loop is working correctly.

How to fix this?

Upvotes: 0

Views: 115

Answers (2)

Kapil Vats
Kapil Vats

Reputation: 5515

you can not perform UI related action in doInBackGround(), like you are strating a new activity in doInBaclground,

 Intent i = new Intent(getApplicationContext(), Index.class);
                    // close the previous activities                    
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);

Upvotes: 0

CQM
CQM

Reputation: 44210

1) you are trying to start an activity within the background thread, don't do this.

2) you are probably getting a json exception, just log more things so you can see what is happening

these may be why it never reaches onPostExecute

Upvotes: 1

Related Questions