Code example for AndroidHttpClient

Methods: closeexecute

0
            result = download();
        } catch (Exception e) {
        	e.printStackTrace();
        } finally { 
            if (client != null) {
                client.close();
            } 
        } 
 
        return result;
    } 
 
    @Override 
    protected void onProgressUpdate(Integer... progress) {
 
        if (progress.length > 1) {
            mTotalSize = progress[1];
            if (mTotalSize == -1) {
                if (mListener != null)
                    mListener.errorDownload(mDownloadInfo);
            } else { 
 
            } 
        } else { 
            mTotalTime = System.currentTimeMillis() - mPreviousTime;
            mDownloadSize = progress[0];
            mDownloadPercent = (mDownloadSize + mPreviousFileSize) * 100 / mTotalSize;
            mNetworkSpeed = mDownloadSize / mTotalTime;
            if (mListener != null)
                mListener.updateProgress(mDownloadInfo);
        } 
    } 
 
    @Override 
    protected void onPostExecute(Long result) {
 
        if (result == -1 || mInterrupt || error != null) {
            if (DEBUG && error != null) {
                Log.v(TAG, "Download failed." + error.getMessage());
            } 
            if (mListener != null) {
                mListener.errorDownload(mDownloadInfo);
            } 
            return; 
        } 
        // finish download 
        mTempFile.renameTo(mFile);
        if (mListener != null)
            mListener.finishDownload(mDownloadInfo);
    } 
 
    @Override 
    public void onCancelled() { 
 
        super.onCancelled(); 
        mInterrupt = true;
    } 
 
    private AndroidHttpClient client;
    private HttpGet httpGet;
    private HttpResponse response;
 
    private long download() throws IOException, NetworkErrorException{ 
 
        if (DEBUG) {
            Log.v(TAG, "mTotalSize: " + mTotalSize);
        } 
 
        /* 
         * check net work 
         */ 
        if (!NetworkUtil.isNetworkAvailable()) { 
            throw new NetworkErrorException("Network blocked.");
        } 
 
        /* 
         * check file length 
         */ 
        client = AndroidHttpClient.newInstance("DownloadTask");
        httpGet = new HttpGet(mUrl);
        response = client.execute(httpGet);
        mTotalSize = response.getEntity().getContentLength();
 
        if (mFile.exists() && mTotalSize == mFile.length()) {
             LogUtil.d(TAG + "Output file already exists. Skipping download.");
 
//            throw new FileAlreadyExistException("Output file already exists. Skipping download."); 
        } else if (mTempFile.exists()) {
            httpGet.addHeader("Range", "bytes=" + mTempFile.length() + "-");
            mPreviousFileSize = mTempFile.length();
 
            client.close();
            client = AndroidHttpClient.newInstance("DownloadTask");
            response = client.execute(httpGet);
 
            if (DEBUG) {
                Log.v(TAG, "File is not complete, download now.");
Connect your IDE to all the code out there  Get Codota for Java