0

非同期タスク用に別のクラスを作成しました。その非同期タスク クラスに文字列値を渡すにはどうすればよいですか? 以下の私のコードを参照してください。

メインクラスで非同期タスククラスを呼び出す

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

2 に答える 2

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 に答える
0

get product_id,av_quantity values inside doInBackground method as :

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

because doInBackground method parameter is Varargs you can get more about Varargs here

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

or second way is you can pass both values by creating an Stock_update constructor as :

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
}

pass both values at time of object creation of Stock_update class :

Stock_updatetask = new Stock_update(product_id,av_quantity);

now you are able to use product_id,av_quantity in whole Stock_update class including doInBackground

于 2013-03-15T10:09:35.820 に答える