Results 1 to 8 of 8

Thread: Java: Passing more than one parameter to Asynctask?

  1. #1
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    838
    Distro
    Ubuntu 17.10 Artful Aardvark

    Java: Passing more than one parameter to Asynctask?

    Hi all,

    I have been looking up this topic for a while but can't come across any simple tutorial/explaination.

    All I want to do is pass a filename into a Asynctask for downloading files. Here is my Asynctask setup currently:

    Code:
    private class DownloadFile extends AsyncTask<String, Integer, String> {
    	    @Override
    	    protected String doInBackground(String... sUrl) {
    	        try {
    //download files etc...
    Anyone have experience doing this?
    Last edited by fallenshadow; April 4th, 2013 at 02:11 PM.
    Projects - PhotoFlare Image Editor | Xwii | URT-2D | BHR
    Hardware - System 76 - Galago UltraPro

  2. #2
    Join Date
    Mar 2010
    Location
    London
    Beans
    924

    Re: Java: Passing more than one parameter to Asynctask?

    Just pass in a file name?

    You've already got it... just call it like:

    Code:
    new DownloadFile().execute("yourFilename");
    It just uses varargs (http://docs.oracle.com/javase/1.5.0/...e/varargs.html)

    The Android docs give you a typical example

    http://developer.android.com/referen...AsyncTask.html
    - "Make me a coffee..."
    - "No"
    - "sudo make me a coffee"
    - "OK"

  3. #3
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    838
    Distro
    Ubuntu 17.10 Artful Aardvark

    Re: Java: Passing more than one parameter to Asynctask?

    I don't think I have it already.

    What im currently doing is passing in a URL but I also want to pass in a filename as a second parameter.

    new DownloadFile().execute("yourFilename");
    Doing this will just make it take the parameter as a URL.
    Projects - PhotoFlare Image Editor | Xwii | URT-2D | BHR
    Hardware - System 76 - Galago UltraPro

  4. #4
    Join Date
    Mar 2010
    Location
    London
    Beans
    924

    Re: Java: Passing more than one parameter to Asynctask?

    Ah ok, I thought you were using the URL for the filename (e.g. "http://someurl.com/<filename>") but you want to pass in the URL separate from the filename?

    So there are two things that come to mind....

    1) Just simply supply two String arguments into the method call, and just know that they should be URL and file name. Varargs will allow this...

    In the task:
    Code:
    @Override
    protected String doInBackground(String... args) {
        // args[0] will be the URL
        // args[1] will be the filename
    }

    Call it by:
    Code:
    new DownloadFile().execute("the url", "the filename");


    2) If there will only be one URL (or one filename) per task, then have an instance variable in your task which stores the value and pass it to the constructor.

    In the task:
    Code:
    private class DownloadFile extends AsyncTask<String, Integer, String> {
    
        private final String url;
    
        DownloadFile(final String url) {
            this.url = url;
        }
    
        @Override
        protected String doInBackground(String... args) {
            // use this.url for the URL
            // use args[0] for the filename
        }
    
       ....
    }
    Call it by:
    Code:
    new DownloadFile("the url").execute("filename");
    There are probably many other ways too...

    Hopefully I've understood what you're after
    Last edited by KdotJ; March 29th, 2013 at 11:39 PM. Reason: typo
    - "Make me a coffee..."
    - "No"
    - "sudo make me a coffee"
    - "OK"

  5. #5
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    838
    Distro
    Ubuntu 17.10 Artful Aardvark

    Re: Java: Passing more than one parameter to Asynctask?

    Thanks!

    It does work the same as args pretty much. I thought it was more complicated than that!

    So this solves the filename but also how can I use the same code to download multiple files?

    I can provide more arguments but it will always take arg0 and arg1. I tried a for loop but that caused my code to crash. :/
    Projects - PhotoFlare Image Editor | Xwii | URT-2D | BHR
    Hardware - System 76 - Galago UltraPro

  6. #6
    Join Date
    Mar 2010
    Location
    London
    Beans
    924

    Re: Java: Passing more than one parameter to Asynctask?

    Without looking at the rest of the code it's hard to say the best way to achieve what you want.

    Quote Originally Posted by fallenshadow View Post
    how can I use the same code to download multiple files?
    - Are all of the files at the same URL?
    - Do you want to download these files concurrently?
    - Can you just use the fully qualified filename? (e.g. "http:/remote.server:1200/path/to/file")

    If all of the files are at the same URL, then I would personally suggest you use something like in my option 2) from the post above. This means the URL will be the same for the entire lifetime of your AsyncTask and you just give it the filenames when you call execute().

    If you want (or can) download the files concurrently, then maybe think about using a separate DownloadTask for each file.


    Quote Originally Posted by fallenshadow View Post
    I can provide more arguments but it will always take arg0 and arg1. I tried a for loop but that caused my code to crash. :/
    Again, without looking at the code it's hard to say.

    A for-loop for multiple args could look like:

    Code:
    for (int i = 0; i < args.length; i+=2) {
        // args[i] is the URL
        // args[i+1] is the filename
    }
    - "Make me a coffee..."
    - "No"
    - "sudo make me a coffee"
    - "OK"

  7. #7
    Join Date
    Mar 2008
    Location
    Ireland
    Beans
    838
    Distro
    Ubuntu 17.10 Artful Aardvark

    Re: Java: Passing more than one parameter to Asynctask?

    Excellent! I did a loop before but I didn't use it in the right place. I also found out saving files was missing a "/".

    I appreciate you helping me out with both issues I was having! Thanks again!
    Projects - PhotoFlare Image Editor | Xwii | URT-2D | BHR
    Hardware - System 76 - Galago UltraPro

  8. #8
    Join Date
    Mar 2010
    Location
    London
    Beans
    924

    Re: Java: Passing more than one parameter to Asynctask?

    Good stuff, hope the rest of the app goes well!
    - "Make me a coffee..."
    - "No"
    - "sudo make me a coffee"
    - "OK"

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •