如何从android中的异步任务返回值?

时间:2013-03-15 11:50:05

标签: android android-asynctask

我有一个单独的异步任务类。如何将值从异步任务类返回到主类。任何人都可以帮助我吗?

在Main类中调用异步任务类

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

异步任务类

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"));

如何将 stock_update 字符串值返回给主类。

} 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();
    }
}

3 个答案:

答案 0 :(得分:0)

您可以创建一个传递给异步类的接口,也可以创建该变量的简单get方法。

答案 1 :(得分:0)

通常我会在AsyncTasks中创建main class。但是如果你将它放在另一个文件中,你可以在asynctask中创建一个getter方法。您可以添加OnPostExecuteListener,当它被调用时,您可以调用成员方法来获取结果。

答案 2 :(得分:0)

回调方法怎么样?

您将主类作为参数传递。然后,在onPostExecute()

中调用主类的一些方法