如何在doInBackground asynctask中放入2个参数?

时间:2014-01-29 06:19:13

标签: java android android-asynctask

我使用Asynctask加载并从php获取数据。我必须将2个参数传递给php。 但我不知道怎么做。

这是java代码:

public class info extends Activity{

ProgressDialog pDialog;
TextView movie_tittle, studio, date;
int std;
String movie, reservation, ttl, dt;

private String URL_CATEGORIES = "http://10.0.2.2/cinemainfo/info.php";

JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> accountsList;
JSONArray accounts = null;

private static final String TAG_SUCCESS = "success";
private static final String TAG_ACCOUNT = "message";

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.info);

    movie = getIntent().getStringExtra("kode_intent"); 
    reservation = getIntent().getStringExtra("kode_intent2");

    movie_tittle=(TextView)findViewById(R.id.tv_tittle);
    date=(TextView)findViewById(R.id.tv_date);
    studio=(TextView)findViewById(R.id.tv_studio);

    new GetCategories().execute();

}

private class GetCategories extends AsyncTask<Void, Void, Void> {

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(info.this);
        pDialog.setMessage("Please Wait..");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    protected Void doInBackground(Void... arg0) {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("id_movie", movie));
        params.add(new BasicNameValuePair("id_reservation", reservation));
        JSONObject json = jParser.makeHttpRequest(URL_CATEGORIES, "GET", params);
        Log.d("All Accounts: ", json.toString());
        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                accounts = json.getJSONArray(TAG_ACCOUNT);
                for (int i = 0; i < accounts.length(); i++) {

                    JSONObject json_data = accounts.getJSONObject(i);

                    ttl=json_data.getString("movie_tittle");

                    dt=json_data.getString("date");

                    std = json_data.getInt("studio");
                }
            }

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

        return null;
    }

    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        result();
    }
}

private void result() {
    try{

        movie_tittle.setText(ttl);
        date.setText(dt);
        studio.setText(String.valueOf(std));
    }

    catch(Exception e){
        Log.e("log_tag","Error in Display!" + e.toString());;          
       }
}

}

我想将id_movieid_reservation传递给php代码。它们来自movie = getIntent().getStringExtra("kode_intent");reservation = getIntent().getStringExtra("kode_intent2");

但是当我在模拟器中运行代码时,它什么也没显示...... php代码很好..但我不确定我的java代码。如何在doInBackground asynctask中传递2个参数?我做错什么了吗 ?

2 个答案:

答案 0 :(得分:2)

String curloc = current.toString();
String itemdesc = item.mDescription;
ArrayList<String> passing = new ArrayList<String>();
passing.add(itemdesc);
passing.add(curloc);
new calc_stanica().execute(passing); //no need to pass in result list
And change your async task implementation

public class calc_stanica extends AsyncTask<ArrayList<String>, Void, ArrayList<String>> {
ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(baraj_mapa.this);
        dialog.setTitle("Calculating...");
        dialog.setMessage("Please wait...");
        dialog.setIndeterminate(true);
        dialog.show();
    }

    protected ArrayList<String> doInBackground(ArrayList<String>... passing) {
        ArrayList<String> result = new ArrayList<String>();
        ArrayList<String> passed = passing[0]; //get passed arraylist

        //Some calculations...

        return result; //return result
    }

    protected void onPostExecute(ArrayList<String> result) {
        dialog.dismiss();
        String minim = result.get(0);
        int min = Integer.parseInt(minim);
        String glons = result.get(1);
        String glats = result.get(2);
        double glon = Double.parseDouble(glons);
        double glat = Double.parseDouble(glats);
        GeoPoint g = new GeoPoint(glon, glat);
        String korisni_linii = result.get(3);
    }

答案 1 :(得分:2)

通话:

String[] arrayOfValue = new String[2];
arrayOfValue[0] = movie;
arrayOfValue[1] = reservation;

new GetCategories().execute(arrayOfValue);

用法:

protected ArrayList<String> doInBackground(String... passing){
String movie = passing[0];
String reservation = passing[1];
}