0

I tried to pass parameters and get out put from asyntask class. Here is my asyntask class

public class GetDataFromAsyncTask extends AsyncTask<String, Integer, Object>{
        @Override
    protected Object doInBackground(String... strurl) {
        // TODO Auto-generated method stub
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet getReq = new HttpGet(strurl[0]);
        ResponseHandler<String> handler = new BasicResponseHandler();

       // HttpResponse response;
        Object responseString = null;
        try {
            //response = httpclient.execute(new HttpGet(strurl[0]));
            HttpResponse execute = httpclient.execute(getReq);
            responseString = httpclient.execute(getReq,handler);           
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
        protected void onPostExecute(Object result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
        }

}

And I am trying to call like below from another class

String url = "some url";
        //GetAsynTask 
        Object strResult=null;
        strResult = new GetDataFromAsyncTask().execute(url);
        if(strResult!=null)
        {
            JSONArray objAry;
            try {
                String data = (String)strResult;
                objAry = new JSONArray(data);
                 }
        }

But above code is not working. When debugging source not found error came. Can anybody help on this... Thanks in advance...

3 Answers3

0

You need to implement an interface class. An interface class will allow you to create a call back function which your Async thread will call once its completed. You can find how to do this here

EDIT - Changed link to a more straight forward outline

1) Create a new interface. To do this, make a new class that contains the following code:

public interface AsyncResponse {
    void processFinish(Object output);
}

2) Add the following to your GetDataFromAsyncTask class:

public class GetDataFromAsyncTask extends AsyncTask{
public AsyncResponse delegate=null; // Creates Instance of your interface

   @Override
   protected void onPostExecute(Object result) {
      delegate.processFinish(result); // Calls processFinish in the interface
   }

3) Add the following to whatever activity is calling:

public class MainActivity implements AsyncResponse{ //implement AsyncResponse
   GetDataFromAsyncTask strResult = new GetDataFromAsyncTask.execute(url);
    @Override
    public void onCreate(Bundle savedInstanceState) {
     strResult.delegate = this;
    }

//This method will run once the thread returns
   void processFinish(Object output){
    if(strResult!=null)
    {
        JSONArray objAry;
        try {
            String data = (String)strResult;
            objAry = new JSONArray(data);
             }
    }
   }
}

Hope this helps, good luck.

Community
  • 1
  • 1
Matt Halleran
  • 25
  • 1
  • 6
0

You have two options:

  1. The easier one is two make your AsyncTask class an inner class of the class that calls the AsyncTask. Then you can simply set the value of strResult variable right in the onPostExecute() method of the AsyncTask class.
  2. If you want to keep your AsyncTask class as a separate top level class, you should follow the method suggested by Matty. Add an interface to the AsyncTask class and let your calling class implement this interface so it can get the result of the AsyncTask once the task is done
Price
  • 2,683
  • 3
  • 17
  • 43
0

put this Async class inside your activity class and you will get data onPostExecute(String result) method. Also make sure you added internet permission in your manifest file.

class GetDataFromAsyncTask extends AsyncTask<String, Integer, String>{
        @Override
    protected Object doInBackground(String... strurl) {
       String resutString = "";
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();        
        HttpGet getReq = new HttpGet(strurl[0]);
      HttpResponse response = client.execute(getReq);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();

            if (statusCode == 200) {
                HttpEntity entityResponse = response.getEntity();
                InputStream content = entityResponse.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    builder.append(line + "\n");
                }
                reader.close();
                resutString = builder.toString();
                Log.d(TAG, "Successfuly :" + resutString);
            } else {
                Log.d(TAG, "Error seding data");
            }
        } catch (ConnectTimeoutException e) {
            Log.w("Connection Tome Out", e);
        } catch (ClientProtocolException e) {
            Log.w("ClientProtocolException", e);
        } catch (SocketException e) {
            Log.w("SocketException", e);
        } catch (IOException e) {
            Log.w("IOException", e);
        } /*catch (JSONException e) {
            e.printStackTrace();
        }*/
        return resutString;
    }

    @Override
        protected void onPostExecute(String result) {            
            super.onPostExecute(result);
            strResult = result;
            if(strResult!=null)
            {
                  JSONArray objAry;
                  try {
                  String data = (String)strResult;
                  objAry = new JSONArray(data);
                  }catch(JSONException e){}

             }
        }

}

call this code like this

new GetDataFromAsyncTask().execute(url)
Ali
  • 1,857
  • 24
  • 25