Lazy Ninja
Lazy Ninja

Reputation: 22527

HttpURLConnection setReadTimeout not working

I am developing an application which requires downloading data from the server.
I use the following code, which works except that it got stuck sometimes at the midst of a file download.

try{
    URL url = new URL( dlUrl );

    con = (HttpURLConnection) url.openConnection();
    con.setConnectTimeout(1000); // timeout 1 sec
    con.setReadTimeout(1000); // timeout 1 sec

    // get file length
    int lenghtOfFile = con.getContentLength();

    is = url.openStream();
    String dir =  Environment.getExternalStorageDirectory() + "myvideos";

    File file = new File( dir );
    if( !file.exists() ){
        if( file.mkdir()){
            // directory succesfully created
        }
    }

    fos = new FileOutputStream(file + "/" +  "video.mp4");
    byte data[] = new byte[1024];

    long total = 0;
    while( (count = is.read(data)) != -1 ){
        total += count;
        publishProgress((int)((total*100)/lenghtOfFile));
        fos.write(data, 0, count);
    }
} catch (Exception e) {
    Log.e(TAG, "DOWNLOAD ERROR = " + e.toString() );
}
finally{
 // close streams
}

The problem could be that the WIFI connection I am using is unstable, or something missing with my code.
Now I want to add a work around when the download stops, but unfortunately setReadTimeout seems to have no effect!
I tried the solutions suggested in Stackoverflow but none did the job for me.
Am I missing some kind of settings?
Any ideas why setReadTimeout has no effect?

Upvotes: 4

Views: 4350

Answers (3)

selbie
selbie

Reputation: 104514

This is a new answer to year-old question, but I had a similar issue in my code that I have been able to resolve.

This line is the problem:

is = url.openStream();

The correct way to obtain the input stream is to simply get the input stream from the connection object not the url object.

is = con.getInputStream();

The former approach likely opens up another network conenction seperate from the connection object obtained by calling url.openConnection()

I figured all this out by evaluating this web blog page.

And for anyone else out there with a similar issue, it's also very important to call setReadTimout early - before calling the getInputStream or connect method on the connection object.

Upvotes: 3

dipali
dipali

Reputation: 11188

HttpPost httpPostRequest = new HttpPost(URL); 
httpPostRequest.getParams().setParameter("http.socket.timeout", new Integer(600000));

please check this code.its working in my app.

Upvotes: 0

loeschg
loeschg

Reputation: 30581

Perhaps a bit of a cop-out, but you might look into using a network library such as http://loopj.com/android-async-http/. I've found it helps alleviate a lot of tedious network debugging such as what you're describing.

The following is an example from the site:

AsyncHttpClient client = new AsyncHttpClient();
String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" };
client.get("http://example.com/file.png", new BinaryHttpResponseHandler(allowedContentTypes) {
    @Override
    public void onSuccess(byte[] fileData) {
        // Do something with the file
    }
});

Upvotes: 0

Related Questions