0

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

在 Main 类中调用异步任务类

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

如何将字符串 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();
    }
}
4

2 回答 2

0

在方法中获取 product_id,av_quantity 值 doInBackground

    //....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_quantityStock_update全班使用,包括doInBackground

于 2013-03-15T10:09:35.820 回答
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
于 2013-03-15T10:13:28.267 回答