Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:31:53+00:00 2026-06-03T03:31:53+00:00

I have a simple code that is supposed to do one task, when the

  • 0

I have a simple code that is supposed to do one task, when the button is clicked download a file, save it to the SD card, and open it. Everything works except during the download, for larger files, the connection drops and the progress bar hangs – almost always at 20%. I have searched and searched and cannot figure out what to do to keep the connection alive and allow the download to complete. Any ideas how to keep the connection alive so the download completes instead of stalling at 20%?

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class DownloadTestActivity extends Activity {

   public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
   private Button startBtn;
   private ProgressDialog mProgressDialog;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
           mProgressDialog = new ProgressDialog(DownloadTestActivity.this);
           mProgressDialog.setMessage("Downloading...");
           mProgressDialog.setIndeterminate(false);
           mProgressDialog.setMax(100);
           mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

           startBtn = (Button) findViewById(R.id.startBtn);
           startBtn.setOnClickListener(new OnClickListener() {
                   public void onClick(View v) {
                           startDownload();
                   }
           });
   }

   private void startDownload() {
           DownloadFile downloadFile = new DownloadFile();
           downloadFile
                           .execute("http://www.website.com/file.pdf");

   }

   class DownloadFile extends AsyncTask<String, Integer, String> {

           @Override
           protected void onPreExecute() {
                   super.onPreExecute();
                   mProgressDialog.show();
           }

           @Override
           protected void onProgressUpdate(Integer... progress) {
                   super.onProgressUpdate(progress);
                   mProgressDialog.setProgress(progress[0]);
           }

           @Override
           protected String doInBackground(String... aurl) {
                   try {
                           URL url = new URL(aurl[0]);
                           URLConnection connection = url.openConnection();
                           connection.connect();

                           int fileLength = connection.getContentLength();
                           Log.d("ANDRO_ASYNC", "Lenght of file: " + fileLength);

                           InputStream input = new BufferedInputStream(url.openStream());
                           String path = Environment.getExternalStorageDirectory()
                                           + "/Android/Data/"
                                           + getApplicationContext().getPackageName() + "/files";
                           File file = new File(path);
                           file.mkdirs();
                           File outputFile = new File(file, "test1.doc");
                           OutputStream output = new FileOutputStream(outputFile);

                           byte data[] = new byte[8192];
                           long total = 0;
                           int count;
                           while ((count = input.read(data)) != -1) {
                                   total += count;
                                   publishProgress((int) (total * 100 / fileLength));
                                   output.write(data, 0, count);
                           }

                           output.flush();
                           output.close();
                           input.close();
                           showPdf();

                   } catch (Exception e) {
                   }
                   return null;
           }

           private void showPdf() {
                   // TODO Auto-generated method stub
           if(mProgressDialog != null){
                   mProgressDialog.dismiss();
           }

                   File file = new File(Environment.getExternalStorageDirectory()
                                   + "/Android/Data/"
                                   + getApplicationContext().getPackageName()
                                   + "/files/test1.doc");
                   PackageManager packageManager = getPackageManager();
                   Intent testIntent = new Intent(Intent.ACTION_VIEW);
                   testIntent.setType("application/msword");
                   List list = packageManager.queryIntentActivities(testIntent,
                                   PackageManager.MATCH_DEFAULT_ONLY);
                   Intent intent = new Intent();
                   intent.setAction(Intent.ACTION_VIEW);
                   Uri uri = Uri.fromFile(file);
                   intent.setDataAndType(uri, "application/msword");
                   startActivity(intent);
           }


   }
}

Edit with logcat information:

--------- beginning of /dev/log/system
W/InputManagerService(  199): Window already focused, ignoring focus gain of:     com.android.internal.view.IInputMethodClient$Stub$Proxy@4137b898
--------- beginning of /dev/log/main
D/AndroidRuntime(20673): 
D/AndroidRuntime(20673): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime(20673): CheckJNI is OFF
D/AndroidRuntime(20673): Calling main entry com.android.commands.pm.Pm
D/AndroidRuntime(20673): Shutting down VM
D/dalvikvm(20673): GC_CONCURRENT freed 101K, 82% free 467K/2560K, paused 1ms+0ms
D/dalvikvm(20673): Debugger has detached; object registry had 1 entries
I/AndroidRuntime(20673): NOTE: attach of thread 'Binder Thread #3' failed
D/AndroidRuntime(20687): 
D/AndroidRuntime(20687): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime(20687): CheckJNI is OFF
D/AndroidRuntime(20687): Calling main entry com.android.commands.am.Am
I/ActivityManager(  199): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.myapp/.Activity} from pid 20687
D/AndroidRuntime(20687): Shutting down VM
D/dalvikvm(20687): GC_CONCURRENT freed 102K, 81% free 489K/2560K, paused 0ms+0ms
D/jdwp    (20687): Got wake-up signal, bailing out of select
D/dalvikvm(20687): Debugger has detached; object registry had 1 entries
I/AndroidRuntime(20687): NOTE: attach of thread 'Binder Thread #3' failed
D/dalvikvm(20698): Late-enabling CheckJNI
I/ActivityManager(  199): Start proc com.myapp for activity     com.myapp/.Activity: pid=20698 uid=10102 gids={1015, 3003}
I/dalvikvm(20698): Turning on JNI app bug workarounds for target SDK version 10...
V/PhoneStatusBar(  271): setLightsOn(true)
D/AudioHardware(   98): AudioHardware pcm playback is going to standby.
D/AudioHardware(   98): closePcmOut_l() mPcmOpenCnt: 1
I/ActivityManager(  199): Displayed com.myapp/.Activity: +197ms
W/InputManagerService(  199): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@4199ed28 (uid=10098 pid=20011)
I/ActivityManager(  199): START {cmp=com.myapp/.Choice} from pid 20698
D/AudioHardware(   98): AudioHardware pcm playback is exiting standby.
D/AudioHardware(   98): openPcmOut_l() mPcmOpenCnt: 0
V/PhoneStatusBar(  271): setLightsOn(true)
I/ActivityManager(  199): Displayed com.myapp/.Choice: +93ms
D/ANDRO_ASYNC(20698): Lenght of file: 736768
D/dalvikvm(20698): GC_CONCURRENT freed 103K, 3% free 9403K/9607K, paused 2ms+3ms
W/NetworkStats(  199): found non-monotonic values; saving to dropbox
D/dalvikvm(  199): JIT code cache reset in 5 ms (1048440 bytes 13/0)
D/dalvikvm(  199): GC_CONCURRENT freed 1472K, 13% free 24697K/28231K, paused 12ms+14ms
D/AudioHardware(   98): AudioHardware pcm playback is going to standby.
D/AudioHardware(   98): closePcmOut_l() mPcmOpenCnt: 1
I/ActivityManager(  199): Force stopping package com.myapp uid=10102
I/ActivityManager(  199): Killing proc 20698:com.myapp/10102: force stop
W/ActivityManager(  199): Force removing ActivityRecord{41eb1ec0 com.myapp/.Choice}: app died, no saved state
I/InputDispatcher(  199): Dropping event because there is no focused window or focused application.
I/ActivityManager(  199):   Force finishing activity ActivityRecord{418450c8 com.myapp/.Activity}
I/InputDispatcher(  199): Dropping event because there is no focused window or focused application.
W/InputDispatcher(  199): channel '417b41f0     com.myapp/com.myapp/.Activity (server)' ~     Consumer closed input channel or an error occurred.  events=0x8
E/InputDispatcher(  199): channel '417b41f0 com.myapp/com.myapp.Activity (server)' ~ Channel is unrecoverably broken and will be disposed!
W/InputDispatcher(  199): Attempted to unregister already unregistered input channel '417b41f0 com.myapp/com.myapp.Activity (server)'
W/WindowManager(  199): Failed looking up window
W/WindowManager(  199): java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@41c7ffc8 does not exist
W/WindowManager(  199): at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7176)
W/WindowManager(  199): at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7167)
W/WindowManager(  199): at com.android.server.wm.WindowState$DeathRecipient.binderDied(WindowState.java:1545)
W/WindowManager(  199): at android.os.BinderProxy.sendDeathNotice(Binder.java:417)
W/WindowManager(  199): at dalvik.system.NativeStart.run(Native Method)
I/WindowManager(  199): WIN DEATH: null
I/WindowManager(  199): WIN DEATH: Window{4175b280     com.myapp/com.myapp.Choice paused=false}
W/WindowManager(  199): Failed looking up window
W/WindowManager(  199): java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@41758028 does not exist
W/WindowManager(  199): at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7176)
W/WindowManager(  199): at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7167)
W/WindowManager(  199): at com.android.server.wm.WindowState$DeathRecipient.binderDied(WindowState.java:1545)
W/WindowManager(  199): at android.os.BinderProxy.sendDeathNotice(Binder.java:417)
W/WindowManager(  199): at dalvik.system.NativeStart.run(Native Method)
I/WindowManager(  199): WIN DEATH: null
W/InputManagerService(  199): Got RemoteException sending setActive(false) notification to pid 20698 uid 10102
D/dalvikvm(20011): GC_CONCURRENT freed 460K, 32% free 9468K/13767K, paused 3ms+4ms
D/AudioHardware(   98): AudioHardware pcm playback is exiting standby.
D/AudioHardware(   98): openPcmOut_l() mPcmOpenCnt: 0
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:31:54+00:00Added an answer on June 3, 2026 at 3:31 am

    I’ve tried to download your file a few times, looks like download stalls for a few seconds here and there, and it might trigger a timeout in your application. Try to specify timeouts explicitly in range 10-20 seconds.

    private DefaultHttpClient httpclient = new DefaultHttpClient();
    private HttpGet get = new HttpGet("your url comes here");
    
    protected String[] doInBackground(Void... v) {
        HttpParams httpParameters = new BasicHttpParams();
        // set the timeout in milliseconds until a connection is established
        // the default value is zero, that means the timeout is not used 
        int timeoutConnection = 3000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // set the default socket timeout (SO_TIMEOUT) in milliseconds
        // which is the timeout for waiting for data
        int timeoutSocket = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    
        httpclient.setParams(httpParameters);
    
        try {
            HttpEntity entity = httpclient.execute( get ).getEntity();
            FileOutputStream output = new FileOutputStream(outputFile);
            entity.writeTo(output);
            output.close();
            // return something, maybe?
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple bit of code that is supposed to capture the
I have a simple assembly code file called exit.s that looks like the following:
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have a simple jQuery code that runs on a web page that has
I have a simple little code fragment that is frustrating me: HashSet<long> groupUIDs =
I have done a simple code for a slideshow that dynamicly loads images from
I'have written a simple code, that gets accelerometer&orientation meter and display some graphics based
I have the following code that fills dataTable1 and dataTable2 with two simple SQL
I have this code that asks for a username and password. Just a simple
below i have a code that runs in most of my simple programs ..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.