1

I have these strings latRepr and LngRepr and I want to send them to the getDirecoes method. The GetDirecoes class is a AsyncTask.

This is how I start the method:

new GetDirecoes().execute();

So, how do I send those strings to here:

private class GetDirecoes extends AsyncTask<Void, Void, Void> implements Serializable {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {}
Grzegorz Górkiewicz
  • 4,496
  • 4
  • 22
  • 38
alb
  • 347
  • 2
  • 7
  • 24
  • Send these values as parameter in execute() method. – DkThakur Mar 30 '17 at 13:36
  • 1
    Possible duplicate of [How can you pass multiple primitive parameters to AsyncTask?](http://stackoverflow.com/questions/12069669/how-can-you-pass-multiple-primitive-parameters-to-asynctask) – Lucas Ferraz Mar 30 '17 at 13:37

6 Answers6

3
new GetDirecoes().execute(latRepr,LngRepr);

this is how you can pass.

private class GetDirecoes extends AsyncTask<String, Void, Void> implements Serializable {

@Override
protected void onPreExecute() {
    super.onPreExecute();
}

@Override
protected Void doInBackground(String... params) {
  String latRepr=params[0];
  String LngRepr=params[1];
 }
 }
Noorul
  • 3,386
  • 3
  • 32
  • 54
1

Try this

new GetDirecoes().execute(latRepr,LngRepr);

and after that

private class GetDirecoes extends AsyncTask<String, Void, Void> implements Serializable {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(String... params) {
      String latRepr = params[0];
      String LngRepr= params[1];

}
DkThakur
  • 516
  • 2
  • 5
  • 17
0

Use constructor in AsyncTask

private class GetDirecoes extends AsyncTask<Void, Void, Void> implements Serializable {

    String mLat;
    String mLng;
    public GetDirecoes(String lat, String lng){
        mLat = lat;
        mLng = lng;
    }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
}

and pass the value

new GetDirecoes(latRepr,LngRepr).execute();
Kunu
  • 5,078
  • 6
  • 33
  • 61
  • @downvoter Would you care to point out the reason for downvote? – Kunu Mar 30 '17 at 13:41
  • Was not me, but why using a custom constructor when the class itself provides better access to simple structured data? – Michael Mar 30 '17 at 13:44
  • Ofcourse that is another solution. And yeah for that he has to pass the values inside execute() method. – Kunu Mar 30 '17 at 13:49
0

your first Void represent the type of params in doInBackground method.

So you can construct your AsyncTask as this :

private class GetDirecoes extends AsyncTask<String, Void, Void> implements Serializable {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(String... params) {
        String lat = params[0];
        String lng = params[1];
        ...
    }

and then call your AsyncTask like this :

new GetDirecoes().execute(latRepr, lngRepr);

You will find more info there : https://developer.android.com/reference/android/os/AsyncTask.html

milcaepsilon
  • 272
  • 3
  • 16
0

Your GetDirecoes is a class, not a method.

What you can do is create a constructor that takes in two String parameters, and store the variables as fields of that GetDircoes.

private class GetDirecoes {
    private String latRepr;
    private String LngRepr;
    public GetDirecoes(String latRepr, String LngRepr) {
          this.latRepr = latRepr;
          this.LngRepr = LngRepr;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {}
}

Then you should be able to call new GetDirecoes(latRepr,LngRepr).execute();

Ishnark
  • 661
  • 6
  • 14
0

The AsyncTask has generic types which in your case are Void, Void, Void. These 3 types are parameters for the methods

doInBackground(params...)
onProgressUpdate(params...)

and

onPostExecute(Result)

So if your first type would be String (AsyncTask<String, Void, Void>), your doInBackground method would look like

doInBackground(String... params) {
  String firstparam = params[0];
}

Please consider to check if params is not null and it's size greater than 0

It's all documented here

Michael
  • 276
  • 2
  • 10