0

I have a separate class for Asynchronous task. How can I return the values from Asynchronous task class to main class. Can any one help me?

In Main class to call Asynchronous task class

String product_id,av_quantity;
Stock_updatetask = new Stock_update();
Stock_updatetask.execute(product_id,av_quantity);

Asynchronous task Class

public class Stock_update extends AsyncTask<String, Void, String> {

JSONObject json = new JSONObject();

JSONArray jsonarray;

String stock_update;

protected String doInBackground(String... params) {

    try {

        // checkInternetConnection();

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(client.getParams(),20000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 20000);

        HttpResponse response;

        HttpPost post = new HttpPost("http://www.name.in/cakefoodnew/customer/stockUpdate?json=");

        json.put("submenu_id", "" + params[0]);
        json.put("available_quantity", "" + params[1]);
        // Log.v("id", ""+json);

        post.setHeader("json", json.toString());
        StringEntity se = new StringEntity(json.toString());

        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
        post.setEntity(se);
        response = client.execute(post);

        if (response != null) {
            // get a data
            InputStream in = response.getEntity().getContent();
            String a = convertStreamToString(in);
            // Log.v("id", ""+a);

            try {

                jsonarray = new JSONArray("[" + a + "]");
                json = jsonarray.getJSONObject(0);
                stock_update = (json.getString("Success"));

How to return the stock_update string values to main class.

} catch (Exception e) {

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

protected void onPostExecute(String result) {

    /*
     * if (stock_update.equalsIgnoreCase("1")) {
     * 
     * progressDialog.dismiss();
     * Toast.makeText(getApplicationContext(),"Stock updated",
     * 700).show();
     * 
     * }
     * 
     * else if (stock_update.equalsIgnoreCase("0")){
     * 
     * progressDialog.dismiss();
     * Toast.makeText(getApplicationContext(),"Stock not updated",
     * 700).show();
     * 
     * }
     */
}

// Json response
private String convertStreamToString(InputStream is) {
        // TODO Auto-generated method stub
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;

        try {
            while ((line = reader.readLine()) != null) {

                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}
midhunhk
  • 5,560
  • 7
  • 52
  • 83
Yugesh
  • 4,030
  • 9
  • 57
  • 97

3 Answers3

0

You can either make an interface that you pass to the async class or a simple get method for that variable.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
  • @Yugesh A get method simply returns the value you need, but you should go look at some examples of `AsyncTask`s. What you probably need is to simply use the `onPostExecute` method to process the results when they return. – Emil Davtyan Mar 15 '13 at 12:26
0

Normally I create my AsyncTasks in the main class. But if you have it in a different file, you could just create a getter method in your asynctask. You can add an OnPostExecuteListener and when it is called, you call the member method to get the results.

Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
Sebastian Breit
  • 6,137
  • 1
  • 35
  • 53
0

What about the callback method?

You pass the main class as parameter. Than, call some method of your main class in onPostExecute()

Nantaphop
  • 570
  • 2
  • 13