0

I have two AsyncTasks running and the async task that is waiting for the result is just not getting the correct result.

I have a network class that runs like so:

public ArrayList<User> searchForFriends(String activeHash, TelephoneNumber telephone)
{
    Object[] obj = {activeHash, telephone};
    try
    {
        return new SearchForFriendsTelephone().execute(obj).get(Constants.TIMEOUT_TIME, Constants.TIMEOUT_UNIT);
    }
    catch (InterruptedException e)
    {
        return null;
    }
    catch (ExecutionException e)
    {
        return null;
    }
    catch (TimeoutException e)
    {
        return null;
    }
}

private class SearchForFriendsTelephone extends AsyncTask<Object, Void, ArrayList<User>>
{
    @Override
    protected ArrayList<User> doInBackground(Object... searchTelephone)
    {
        if (config.getNetworkVersion() == config.NETWORK_PROTOCOL_VERSION_1)
        {
            TelephoneNumber tel = (TelephoneNumber) searchTelephone[1];
            List<NameValuePair> params = new ArrayList<NameValuePair>(3);
            params.add(new BasicNameValuePair(NetworkConfig.POST_ACTIVE_HASH, (String) searchTelephone[0]));
            params.add(new BasicNameValuePair(NetworkConfig.POST_MOBILE_NUMBER_COUNTRY_CODE, tel.getCountryCode()));
            params.add(new BasicNameValuePair(NetworkConfig.POST_MOBILE_NUMBER_RAW, tel.getNumberRaw()));
            ServerCommunication csc = new ServerCommunication();
            JSONObject jsonFoundFriends = csc.postToServer(config.getBaseUrl() + URL_FRIEND_SEARCH_MOBILE, params);
            if (jsonFoundFriends == null || csc.networkError())
            {
                FriendNetworkCommunication.this.networkError = csc.getNetworkError();
                return null;
            }
            return _processSearchFriends(jsonFoundFriends);
        }
        FriendNetworkCommunication.this.networkError = new NetworkError(NetworkLanguage.UNABLE_TO_PROCESS);
        return null;
    }

Anyway this works fine with no issues and pulls back the user/s. I know this as I tried the following code in the main ui thread and it populates a view just fine. When I call this code from another AsyncTask. I get a timeout error.

Code to all the searchForFriends code:

private class CompareNumbers extends AsyncTask<ArrayList<NameAndNumber>, Integer, Void>
{
    @Override
    protected Void doInBackground(ArrayList<NameAndNumber>... params)
    {
        for (NameAndNumber nameNumber : params[0])
        {
            try
            {
                FriendNetworkCommunication fnc = new FriendNetworkCommunication();
                ArrayList<User> users = fnc.searchForFriends(CurrentUser.getInstance().getUserActiveHash(), new TelephoneNumber(String.valueOf(nameNumber.getNumber().getNationalNumber()), String.valueOf(nameNumber.getNumber().getCountryCode())));
                if (users != null && users.size() == 1)
                {
                    User u = users.get(0);
                    String[] s = nameNumber.getName().split(" ");
                    u.setFirstName(s[0]);
                    u.setLastName(s[1]);
                    ((ArrayAdapter<User>) ((ListView) getView().findViewById(R.id.friend_add_fragment_search_cont_list)).getAdapter()).add(u);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        ((ArrayAdapter<User>) ((ListView)getView().findViewById(R.id.friend_add_fragment_search_cont_list)).getAdapter()).notifyDataSetChanged();
        return null;
    }
 }

Can I not run an asynctask that waits on another?

NOTE: This is all running in a fragment if this makes any difference?

NOTE2: The first Asynctask runs a network call and has to be run asynchronously and so I wanted it to be like this so if I wanted I could run it anywhere synchronously

bubblebath
  • 939
  • 4
  • 18
  • 45
  • Could you please also show your second AsyncTask and how it calls the first one? – Scadge Aug 28 '14 at 10:33
  • A reason for using AsyncTasks is so you don't have to wait. If one task depends on data from another, then it shouldn't be initiated until that other one is complete. – Mike M. Aug 28 '14 at 10:34
  • Look at updated post – bubblebath Aug 28 '14 at 10:35
  • The first Asynctask runs a network call and has to be run asynchronously and so I wanted it to be like this so if I wanted I could run it anywhere synchronously – bubblebath Aug 28 '14 at 10:36
  • 1
    I don't see the point in calling another `AsyncTask` in the `doInBackground()` of an `AsyncTask`. If you want the rest of the code from the first `AsyncTask` to execute only after the second `AsyncTask` has finished, then why use a second `AsyncTask`? – shyam Aug 28 '14 at 10:46
  • @shyam The reason is that sometimes I will make this call in a synchronous mannor and setting a new async task is a real pain in the ass so if it already runs in an AsyncTask then I save myself a lot of trouble – bubblebath Aug 28 '14 at 10:51

2 Answers2

1

try giving the .execute() of the second async task in the onpostexecute() of the first async task.

Karthika.S
  • 29
  • 2
0

I have found the answer to my question and this is not possible.

A full answer can be found here:

Creating another AsyncTask inside of doInBackground

Community
  • 1
  • 1
bubblebath
  • 939
  • 4
  • 18
  • 45