Code example for URLConnection

Methods: getContentLength, getInputStream

0
      connection = mUrl.openConnection();
    } catch (IOException e) {
      throw new Sl4aException("Cannot open URL: " + mUrl, e);
    } 
 
    int contentLength = connection.getContentLength();
 
    if (mFile.exists() && contentLength == mFile.length()) {
      Log.v("Output file already exists. Skipping download.");
      return 0l; 
    } 
 
    try { 
      mProgressReportingOutputStream = new ProgressReportingOutputStream(mFile);
    } catch (FileNotFoundException e) {
      throw new Sl4aException(e);
    } 
 
    publishProgress(0, contentLength);
 
    int bytesCopied = IoUtils.copy(connection.getInputStream(), mProgressReportingOutputStream);
    if (bytesCopied != contentLength && contentLength != -1) {
      throw new IOException("Download incomplete: " + bytesCopied + " != " + contentLength);
    } 
    mProgressReportingOutputStream.close();
    Log.v("Download completed successfully.");