如何将字符串值传递给android中单独的类中的异步任务?

时间:2013-03-15 10:07:26

标签: android android-asynctask

我为异步任务创建了一个单独的类。如何将字符串值传递给该异步任务类?请参考下面的代码。

在Main类中调用异步任务类

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

如何将String product_id,av_quantity值发送到异步任务类

异步任务类

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

JSONObject json = new JSONObject();

JSONArray jsonarray;


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", "" + product_id);
        json.put("available_quantity", "" + av_quantity);*/
        // 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"));

            } catch (Exception e) {

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

    return null;
}
}

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

2 个答案:

答案 0 :(得分:0)

doInBackground方法中的product_id,av_quantity值设为:

    //....your code here...
   json.put("submenu_id", "" + params[0]); //<<<< get product_id
   json.put("available_quantity", "" + params[1]); //<<< get av_quantity
    // Log.v("id", ""+json);

    post.setHeader("json", json.toString());

因为doInBackground方法参数为Varargs,您可以在此处获得有关Varargs的更多信息

http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

或第二种方法是,您可以通过创建Stock_update构造函数来传递两个值:

public class Stock_update extends AsyncTask<String, Void, String> {
 String product_id,av_quantity;
public Stock_update(String product_id,String av_quantity){

   this.product_id=product_id;
   this.av_quantity=av_quantity;
 }
//your code here
}

在创建Stock_update类的对象时传递两个值:

Stock_updatetask = new Stock_update(product_id,av_quantity);

现在您可以在整个product_id,av_quantity课程中使用Stock_update,包括doInBackground

答案 1 :(得分:0)

请参阅此代码

DownloadingProgressTask downloadingProgressTask = new DownloadingProgressTask(
                                    Utilities.arrayRSSDownload.get(0).getUrl(),
                                    mainprogressbar, Utilities.arrayRSSDownload
                                            .get(0).getTitle());
                            downloadingProgressTask.execute();

然后在课堂上

private class DownloadingProgressTask extends
        AsyncTask<String, Integer, Boolean> {

    String fileName;
    ProgressBar progressbar;

    /** progress dialog to show user that the backup is processing. */

    public DownloadingProgressTask(String url1, ProgressBar progress,
            String filetitle) {

        urllink = url1;
        fileName = filetitle;
        progressbar = progress;
    }

    protected void onPreExecute() {

        mainprogressbar.setProgress(0);
        progressbar.setProgress(0);

        myDatabase.updateDownloadStatus(fileName, 2);
        // Updating the home screen list
        setListData();
    }

       --- rest of code