4

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?

1
  • 1
    Better late than never. I think I spotted the issue in your code. See my new answer below. Replace the call to url.openStream with con.getInputStream. Commented Aug 9, 2014 at 8:21

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This blog article is giving 404 so here is cached version. web.archive.org/web/20160119134317/http://…
0

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 } }); 

1 Comment

Is it possible to download video and pdf files using this library?
0
HttpPost httpPostRequest = new HttpPost(URL); httpPostRequest.getParams().setParameter("http.socket.timeout", new Integer(600000)); 

please check this code.its working in my app.

2 Comments

I think it could work. But I am trying to solve HttpURLConnection.setReadTimeout(). Maybe as a last resort, I will try it.
please check..its work or not?if its useful for you,then please accept it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.