Java Question ASYNCTASK NETWORK

mohsen nasrabady

Active Member
Licensed User
B4X:
public abstract class DownloadManager {
    public long downloaded = 0;
    public long total = 0;
    private Object fos;
  
    public void resume(String address,String path) throws Exception{
    URL url = new URL(address);
  
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        File file=new File(path);
    if(file.exists()){
             downloaded = file.length();
             connection.setRequestProperty("Range", "bytes="+(file.length())+"-");
  
    }else{
        connection.setRequestProperty("Range", "bytes=" + downloaded + "-");
    }
    connection.setDoInput(true);
    connection.setDoOutput(true);
    total = connection.getContentLength();
     BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
     fos = (downloaded==0)? new FileOutputStream(path): new FileOutputStream(path,true);
     BufferedOutputStream bout = new BufferedOutputStream((OutputStream) fos, 1024);
    byte[] data = new byte[1024];
    int x = 0;
    while ((x = in.read(data, 0, 1024)) >= 0) {
        bout.write(data, 0, x);
         downloaded += x;
 
    }
    }
  
    }

this code error Caused by: android.os.NetworkOnMainThreadException
i searched the net and seems the error solved if do in background with ASYNCTASK
how can do that?
 
Last edited:

mohsen nasrabady

Active Member
Licensed User
any other method that can setproperty to resume download in downloadlarge file with httputil2? or downloadmanager lib?
 
Top