Java Question Trouble declaring a subclass that extends AsyncTask

thedesolatesoul

Expert
Licensed User
Longtime User
Here is the code that causes the problem:
B4X:
@ShortName("DownloadFileTask")
    public static class DownloadFileTask extends AsyncTask<String, Integer, Boolean> 
    {
      @Override
      protected Boolean doInBackground(String... arg0) {
         String root      = arg0[0];
         String dbPath    = arg0[1];
         String localFile = arg0[2];
         String etag      = arg0[3];
           BufferedInputStream br = null;
         BufferedOutputStream bw = null;
           HttpEntity entity = null;
           String result = "";
           try  {
               FileDownload response = getFileStream(root, dbPath, etag);
               try {
                   br = new BufferedInputStream(response.is);
                bw = new BufferedOutputStream(new FileOutputStream(localFile));
                byte[] buffer = new byte[4096];
                int read;
                while (true) {
                   read = br.read(buffer);
                   if (read <= 0) {
                      break;
                   }
                   bw.write(buffer, 0, read);
                }
               } finally {
                   System.out.println("Result: " + result);
                   if (bw != null) {bw.close();}
                   if (br != null) {br.close();}
               }
           } catch (SocketException se) {
               System.out.println(se.toString());
               Log.w("B4A","Socket Exception");
           } catch (IOException ioe) {
               System.out.println(ioe.toString());
               Log.w("B4A","IO Exception");
           } finally {
               System.out.println("Result: " + result);
           }
         return true;
      }
       
    }

which is part of another class.
I dont see any errors in Eclipse but when I load the library in B4A I get:
object reference not set to an instance of an object

Since it is a static class why do I need to instantiate it?
Maybe it has something to do with AsyncTask?

Thanks in advance!
 

thedesolatesoul

Expert
Licensed User
Longtime User
I get this error when the library is loaded (i.e. I refresh the Libs tab)

Why do you not prefer AsyncTask?

Okay, now let me find submitrunnable.

EDIT:
Looks like runnable creates a new thread.
I need to pass the progress of the thread back to the main thread. How do I do that?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
I will post an example later this week.
You can also use jd-gui and see how the Http library or Net library are built.

A lot of efforts have been made to correctly build Http library and HttpUtils code module. Threading in Android can be pretty complicated as you also need to deal with the activity or service life cycle.

You will get better results by using them instead of trying to build everything from scratch.
For example your code doesn't seem to properly handle errors. The developer will not have a chance to react to possible errors.

The purpose of libraries is to fill missing features. If it can be done in Basic4android code then there are several advantages to build it in Basic4android.

This fully functional Dropbox Example, which takes care of all the required steps, is less than 200 lines of code.
 

thedesolatesoul

Expert
Licensed User
Longtime User
I will post an example later this week.
You can also use jd-gui and see how the Http library or Net library are built.
Wow! I didnt know JAR files can be decompiled to source like that!
Does that mean any Android apk can be decompiled like this?

A lot of efforts have been made to correctly build Http library and HttpUtils code module. Threading in Android can be pretty complicated as you also need to deal with the activity or service life cycle.
Yes, I can see that the HTTP Library is VERY well written and I have nothing but :sign0188::sign0188::sign0188:

This fully functional Dropbox Example, which takes care of all the required steps, is less than 200 lines of code.
Yes, this is too easy to use :) ...but also the one thing HTTP libraries do not handle yet is FileUpload/Download progress. I have to say, for a Dropbox client this is fairly important esp as it could take some time for the transactions to complete.

Anyways, I can see that the HTTP library does give out an OutputStream on downloads, but it is written to in one go:
B4X:
            HttpClientWrapper.HttpResponeWrapper.this.response.getEntity().writeTo(this.val$Output);
            if (this.val$CloseOutput)
              this.val$Output.close();
I believe this statement will be blocked until the file is downloaded.
This doesnt allow monitoring of the download. Maybe it is worth adding another function GetAsynchronouslyWithProgressMonitor, that actually writes to the OutputStream in smaller increments and updates another variable for progress?

I cant see how to do this for Uploading.
There is an InputStream for that but I cannot see how that works.
 

thedesolatesoul

Expert
Licensed User
Longtime User
I have plans to add "counting" input/output streams which will allow to monitor the progress of operations that work with streams, like this one.
I expect it to be pretty simple. Though I'm always ready for surprises...
For someone like you, it will be a breeze...the only surprises you will have: That was way too easy! :)
 
Top