-2

What is the proper way in the following code (it's a bit complicated structure to me) to get url from the method gotUrl() to the doInBackground() method of AsyncTask in order to use it in onPostExecute(), after the doInBackground() method has completed its task?

public class PlayerActivity extends CustomActivity implements 
                                              ProblemListener{

     public class PlayChannel extends 
                      AsyncTask<CustomChangeChannel, String, String> {

          @Override
          protected String doInBackground(CustomChangeChannel... params) {
                 initOctoshapeSystem();

          return url;
          } 

          @Override
          protected void onPostExecute(String url){ 

          }

     }


     public void initOctoshapeSystem() {
           os = OctoStatic.create(this, this, null);

           os.setOctoshapeSystemListener(new OctoshapeSystemListener() {

                @Override
                public void onConnect() {
                     mStreamPlayer = setupStream(OCTOLINK);
                     mStreamPlayer.requestPlay();
                }
          });
     }



     public StreamPlayer setupStream(final String stream) {
        StreamPlayer sp = os.createStreamPlayer(stream);
        sp.setStatusChangedListener(new StatusChangedListener() {
            @Override
            public void statusChanged(byte oldStatus, 
                                      final byte newStatus) {
                runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                    //todo
                   }
                });
             }
        });

        sp.setListener(new StreamPlayerListener() {

            @Override
            public void gotUrl(String url) {
                //String to be passed
            }
        });

     return sp;
     }
}
Desaretiuss
  • 311
  • 1
  • 3
  • 12

2 Answers2

3
AsyncTask<Param1, Param2, Param3>

Param 1 is the param that you pass into your doInBackground method.

Param 2 is what you want to get while the AsyncTask working.

Param 3 is what you want to get as result.

You can declare all them as Void.

AsyncTask<Void, Void, Void>

In your case you want to pass String URL to your doInBackground, so:

AsyncTask<String, Void, Void>

Pass your URL String when you call the execute.

mAsyncTask.execute("your url");

Then get it in the doInBackground:

protected Void doInBackground(String... params) {
    String yourURL = params[0];
    return null;
}
Alex Kamenkov
  • 891
  • 1
  • 6
  • 16
0

change this

 public class PlayChannel extends 
                  AsyncTask<CustomChangeChannel, String, String>

to this

 public class PlayChannel extends 
                  AsyncTask<String, String, String>

and then use

PlayChannel channel  = new PlayChannel(url);
AJay
  • 1,233
  • 10
  • 19