Rishabh Rishabh - 3 months ago 11
Android Question

How to return back the object using async task?

I am trying to return back the list of tweets after the parsing from async task ....
But I am not getting the arraylist back from the task .
Can anyone suggest a solution?

public class Main extends ListActivity {
String MY_APP_TAG = "com.list";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList listItems = new ArrayList();
new myAsyncTask().execute(listItems);

setListAdapter(new ArrayAdapter(this, R.layout.tweet, R.id.tweet,listItems));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a Toaster
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}

private class myAsyncTask extends AsyncTask<ArrayList<Object>, Void, Void>
{
ArrayList<Object> listItems;
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(Main.this, "", "Loading....");
}
@Override
protected Void doInBackground(ArrayList<Object>... params) {

String host = "api.twitter.com";
String twitterURL = "http://"+host+"/1/statuses/user_timeline.json?screen_name=i1990jain&amp;count;=10";
try {
HttpClient client = new DefaultHttpClient();
BasicHttpContext localContext = new BasicHttpContext();
HttpHost targetHost = new HttpHost(host, 80, "http");
HttpGet httpget = new HttpGet(twitterURL);
httpget.setHeader("Content-Type", "application/json");
HttpResponse response = client.execute(targetHost, httpget, localContext);
HttpEntity entity = response.getEntity();
Object content = EntityUtils.toString(entity);
Log.d(MY_APP_TAG, "OK: " + content.toString());

JSONArray ja = new JSONArray(content.toString());

for(int i = 0; i < ja.length(); i++){
JSONObject jo = ja.getJSONObject(i);
listItems.add(jo.getString("text"));
}
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
dialog.dismiss();
}
}
}

Answer

how I got it working

public class Main extends ListActivity {

    String MY_APP_TAG = "com.vidyut";
    ArrayList<Object> list = new ArrayList<Object>();
        /** Called when the activity is first created. */
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myAsyncTask task = new myAsyncTask();
        task.execute();

}

    private class myAsyncTask extends AsyncTask<ArrayList<Object>, ArrayList<Object>, ArrayList<Object>>
    {
        ProgressDialog dialog;
        @Override
        protected void onPreExecute() {
            dialog = ProgressDialog.show(Main.this, "", "Loading....");
        }

        @Override
        protected ArrayList<Object> doInBackground(ArrayList<Object>... params) {

            String host = "api.twitter.com";
            String twitterURL = "http://"+host+"/1/statuses/user_timeline.json?screen_name=i1990jain&amp;count;=10";
         try {
          HttpClient client = new DefaultHttpClient();
             BasicHttpContext localContext = new BasicHttpContext();
             HttpHost targetHost = new HttpHost(host, 80, "http");
             HttpGet httpget = new HttpGet(twitterURL);
             httpget.setHeader("Content-Type", "application/json");
             HttpResponse response = client.execute(targetHost, httpget, localContext);
             HttpEntity entity = response.getEntity();
             Object content = EntityUtils.toString(entity);
             Log.d(MY_APP_TAG, "OK: " + content.toString());

             JSONArray ja = new JSONArray(content.toString());

          for(int i = 0; i < ja.length(); i++){
           JSONObject jo = ja.getJSONObject(i);
           list.add(jo.getString("text"));
          }
         } catch(Exception e) {
          e.printStackTrace();
         }
            return list;
        }
        protected void onPostExecute(ArrayList<Object> list) {
            dialog.dismiss();

             Log.d(MY_APP_TAG, "The returned list contains " +list.size()+ "elements");
             setListAdapter(new ArrayAdapter<Object>(Main.this, R.layout.tweet, R.id.tweet,list));
                ListView lv = getListView();
                lv.setTextFilterEnabled(true);
                lv.setOnItemClickListener(new OnItemClickListener() {
                 @Override
                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                  // When clicked, show a Toaster
                      Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
             }
            });

        }

    }
}