Android
Android

Reputation: 9023

android async task in for loop

i want to call async task from foor loop how i can do this? i have done this

for (int i = 0; i < listimage.size(); i++) {
            GlobalVariable.set_URL(listimage.get(i) + "");
            System.out.println("from fav " + GlobalVariable.get_URL() + "");
            try {
                 obj = new JSONObject(GlobalVariable.get_URL());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            GetContacts gc = new GetContacts();
            gc.execute();

        }

my logcat:

04-08 14:27:33.894: E/AndroidRuntime(2526): FATAL EXCEPTION: AsyncTask #5
04-08 14:27:33.894: E/AndroidRuntime(2526): java.lang.RuntimeException: An error occured while executing doInBackground()
04-08 14:27:33.894: E/AndroidRuntime(2526):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.lang.Thread.run(Thread.java:856)
04-08 14:27:33.894: E/AndroidRuntime(2526): Caused by: java.lang.NullPointerException
04-08 14:27:33.894: E/AndroidRuntime(2526):     at com.leeway.hdwallpaper.favourite_Activity$GetContacts.doInBackground(favourite_Activity.java:182)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at com.leeway.hdwallpaper.favourite_Activity$GetContacts.doInBackground(favourite_Activity.java:1)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-08 14:27:33.894: E/AndroidRuntime(2526):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-08 14:27:33.894: E/AndroidRuntime(2526):     ... 5 more
04-08 14:27:34.305: E/WindowManager(2526): Activity com.leeway.hdwallpaper.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42642538 that was originally added here
04-08 14:27:34.305: E/WindowManager(2526): android.view.WindowLeaked: Activity com.leeway.hdwallpaper.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42642538 that was originally added here
04-08 14:27:34.305: E/WindowManager(2526):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
04-08 14:27:34.305: E/WindowManager(2526):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:297)
04-08 14:27:34.305: E/WindowManager(2526):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:229)
04-08 14:27:34.305: E/WindowManager(2526):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:154)
04-08 14:27:34.305: E/WindowManager(2526):  at android.view.Window$LocalWindowManager.addView(Window.java:547)
04-08 14:27:34.305: E/WindowManager(2526):  at android.app.Dialog.show(Dialog.java:285)
04-08 14:27:34.305: E/WindowManager(2526):  at com.leeway.hdwallpaper.favourite_Activity$GetContacts.onPreExecute(favourite_Activity.java:161)
04-08 14:27:34.305: E/WindowManager(2526):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
04-08 14:27:34.305: E/WindowManager(2526):  at android.os.AsyncTask.execute(AsyncTask.java:534)
04-08 14:27:34.305: E/WindowManager(2526):  at com.leeway.hdwallpaper.favourite_Activity.onCreateView(favourite_Activity.java:108)
04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
04-08 14:27:34.305: E/WindowManager(2526):  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
04-08 14:27:34.305: E/WindowManager(2526):  at android.os.Handler.handleCallback(Handler.java:643)
04-08 14:27:34.305: E/WindowManager(2526):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 14:27:34.305: E/WindowManager(2526):  at android.os.Looper.loop(Looper.java:137)
04-08 14:27:34.305: E/WindowManager(2526):  at android.app.ActivityThread.main(ActivityThread.java:4803)
04-08 14:27:34.305: E/WindowManager(2526):  at java.lang.reflect.Method.invokeNative(Native Method)
04-08 14:27:34.305: E/WindowManager(2526):  at java.lang.reflect.Method.invoke(Method.java:511)
04-08 14:27:34.305: E/WindowManager(2526):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-08 14:27:34.305: E/WindowManager(2526):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-08 14:27:34.305: E/WindowManager(2526):  at dalvik.system.NativeStart.main(Native Method)

This is my getcontact.doinbackground

protected Void doInBackground(Void... params) {
    sample = new ArrayList<String>();

    contactList = new ArrayList<HashMap<String, String>>();
    gv = (GridView) getActivity().findViewById(R.id.gridView1);

    ServiceHandler sh = new ServiceHandler();


    String jsonStr = sh.makeServiceCall(obj.toString(),
            ServiceHandler.GET);
    System.out.println("this is from do in.."
            + GlobalVariable.get_URL() + "");
    Log.d("Response: ", "> " + jsonStr);

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);

            // Getting JSON Array node
            contacts = jsonObj.getJSONArray(TAG_WALLPAPER);

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

                String id = c.getString(TAG_ID);

                // Phone node is JSON Object

                JSONObject phone = c.getJSONObject(TAG_IMAGE);

                String thumb = phone.getString(TAG_THUMB);
                String android = phone.getString(TAG_IMAGE_ANDROID);

                // img_url.add(thumb);
                // grdadpter ga = new grdadpter(getActivity(), sample);
                // gv.setAdapter(ga);
                // String office = phone.getString(TAG_IMAGE_OFFICE);

                // tmp hashmap for single contact
                HashMap<String, String> contact = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                contact.put(TAG_ID, id);
                img_ID.add(id);
                // adding contact to contact list
                contactList.add(contact);
                // sample.add(thumb);
                publishProgress(thumb);
                android_img.add(android);
                System.out.println("this is from do in ng.."
                        + sample.size());

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }

    return null;

}

Upvotes: 0

Views: 1952

Answers (4)

Praveena
Praveena

Reputation: 6941

AsynTask in a for loop? Your for loop will not wait for AsyncTask to complete. And at a given time maximum of only 5 AsynTask is allowed.

Upvotes: 1

Mario
Mario

Reputation: 145

for (int i = 0; i < listimage.size(); i++) {
            GlobalVariable.set_URL(listimage.get(i) + "");
            System.out.println("from fav " + GlobalVariable.get_URL() + "");
            try {
                 obj = new JSONObject(GlobalVariable.get_URL());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            GetContacts gc = new GetContacts();
            gc.execute();

        }

change to

for (int i = 0; i < listimage.size(); i++) {
            GlobalVariable.set_URL(listimage.get(i) + "");
            System.out.println("from fav " + GlobalVariable.get_URL() + "");
            try {
                 obj = new JSONObject(GlobalVariable.get_URL());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            GetContacts gc[i] = new GetContacts();
            gc[i].execute();

        }

Upvotes: 0

nivensookharan
nivensookharan

Reputation: 193

Because you not returning anything in your async call wouldn't it be

new GetContacts().execute();

or you need to adjust your async call to return contacts??

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

I guess you have not initilize img_ID ArrayList initilize it inside your doInBackground like below.

img_ID = new ArrayList<String>();

Edit:

same like that you can not initilize you GridView inside doInBackground remove it do that in Activity or Fragment

Upvotes: 0

Related Questions