Ritesh gupta
Ritesh gupta

Reputation: 35

AsyncTask with ArrayList as return type

I am trying to return an ArrayList from an AsyncTask. But in main activity i am not able to get the returned arraylist. I want to know the way to call the asynctask so that i can get the returned arraylist.

This is the AsyncTask -

public class GetBannerDB extends AsyncTask<Void, Void, ArrayList<String>> {

GetBannerDB() {
}

@Override
protected ArrayList<String> doInBackground(Void... params) {
    InputStream is = null;
    ArrayList<String> bannerList =  new ArrayList<String>();

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("data", "data"));

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("url to fetch data");
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpClient.execute(httpPost);

        HttpEntity entity = response.getEntity();

        is = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        serverResult = sb.toString().trim();

        try {
            String sResult = serverResult.substring(0, 7);

            if (sResult.equalsIgnoreCase("success")) {
                // Creating user login session
                // Use user real data
                String[] temp = serverResult.split("\\^");

                String banner_image = temp[1];
                String banner_redirect = temp[2];

                bannerList.add(banner_image);
                bannerList.add(banner_redirect);

            } else {

            }
        } catch (NullPointerException ex) {

        }

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bannerList;
}

@Override
protected void onPostExecute(ArrayList<String> result) {
    super.onPostExecute(result);
    mBannerTask = null;
}
}

In MainActivity, i am calling the output as --

ArrayList<String> adDetails = new ArrayList<>();

mBannerTask = new GetBannerDB();
adDetails = mBannerTask.execute();

But here i am getting error - Incompatible types.

Where m i going wrong pls guide.

Ashok as per your answer, i have done the following changes --

OnDataListener.java (As interface)

public interface OnDataListener {
    void setData(ArrayList<String> result);
}

In MainActivity.java -

public class PushAds implements OnDataListener{
    String serverResult = null;
    private GetBannerDB mBannerTask = null;
    ArrayList<String> adDetails = new ArrayList<>();

    public void displayAds()
    {
        mBannerTask  = new GetBannerDB(new OnDataListener {

            public void setData(ArrayList<String> result){

                adDetails   = result;
            }

        });
        mBannerTask.execute();
    }
    ......
}

But its giving error.

Upvotes: 1

Views: 55

Answers (1)

ashok
ashok

Reputation: 429

public interface OnDataListner{
  void setData(ArrayList<String> result); 
 }

and

public class GetBannerDB extends AsyncTask<Void, Void, ArrayList<String>> {
   public OnDataListner dataListner;

  GetBannerDB(OnDataListner dataListner) { 
      this.dataListner = dataListner;
  }

   @Override
  protected void onPostExecute(ArrayList<String> result) {
    super.onPostExecute(result);
    dataListner.setData(result);

  }
}

finally

    ArrayList<String> adDetails = new ArrayList<>();

   mBannerTask  = new GetBannerDB(new OnDataListner{

     @Overrid
    public void setData(ArrayList<String> result){

         adDetails   = result;
         //do something

        }

  });
 mBannerTask.execute();

Upvotes: 1

Related Questions