Aprilia
Aprilia

Reputation: 53

How to put 2 parameters in doInBackground asynctask ?

I use Asynctask to load and get data from php. And I have to pass 2 parameters to php. But I don't know how.

Here is the java code :

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

}

I want to pass id_movie and id_reservation to php code..Both is getting from movie = getIntent().getStringExtra("kode_intent"); and reservation = getIntent().getStringExtra("kode_intent2");

But when I run the code in emulator, It displays nothing..The php code is fine..But I'm not sure with my java code. How to pass 2 parameters in doInBackground asynctask? Did I do something wrong ?

Upvotes: 1

Views: 2836

Answers (2)

Gaurav Gupta
Gaurav Gupta

Reputation: 4691

Calling:

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

new GetCategories().execute(arrayOfValue);

Usage:

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

Upvotes: 2

Digvesh Patel
Digvesh Patel

Reputation: 6533

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

Upvotes: 2

Related Questions