CR Sardar
CR Sardar

Reputation: 1026

HttpURLConnection reading input streams fails

I have a file of size 5MB & want to download through a HttpURLConnection ( target is like "http://...../songs/Beatles_And I Love Her.mp3").

I'm trying to do it as follows

URL url = new URL("http://........../songs/Beatles_And I Love Her.mp3");

URLConnection urlConnection = url.openConnection(); 
HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;

httpURLConnection.getResponseCode(); 
InputStream stream = httpURLConnection.getInputStream();

byte buf[] = new byte[128]; 
do 
{ 
    int numread = stream.read(buf); 
    if (numread <= 0)
    { 
        stream.close(); 
        break; 
    }
} while (true);

But the problem is that it only reads only 2KB to 3KB of data and after that it is returning -1 and exiting.

Upvotes: 1

Views: 1657

Answers (5)

user2024792
user2024792

Reputation: 41

You have just for got to call following before creating InputStream from it

if(httpURLConnection.getResposecode==HTTPConnection.OK){
    // Do your job here
}else{
   //Error in connection creation
}

Upvotes: 1

Jacob Nordfalk
Jacob Nordfalk

Reputation: 3533

Change

if (numread <= 0)

to

if (numread < 0)

As stated in the javadoc you should only break out of the loop when you get a -1:

"the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached."

Getting 0 bytes read is a little weird, but might happen because the data has been split up in packets in the underlying network layer.

Upvotes: 0

Jacob Nordfalk
Jacob Nordfalk

Reputation: 3533

Check the response code (should be 200) and check the content of the data you get.

Ive had a similar problem. Turned out it was an error page from the server I read, not the correct file :-)

Also, try with a file that doesen't contain spaces (rename to Beatles_And_I_Love_Her.mp3 on the server)

Upvotes: 1

jazz
jazz

Reputation: 1216

increase your buffer size

byte buf[] = new byte[2048]; 

Upvotes: 0

Dharmendra
Dharmendra

Reputation: 34016

Try this it is working for me

try{
            if(path != null && fileName != null){
                Log.v(TAG, "downloading data");

                URL url  = new URL(path + fileName);
                URLConnection conexion = url.openConnection();
                conexion.connect();

                int lenghtOfFile = conexion.getContentLength();
                Log.v(TAG, "lenghtOfFile = "+lenghtOfFile);

                InputStream is = url.openStream();

                File testDirectory = new File(Environment.getDataDirectory().toString() + "/data/" + c.getPackageName() + "/download");
                if(!testDirectory.exists()){
                    testDirectory.mkdir();
                }
                FileOutputStream fos = new FileOutputStream(testDirectory+"/" + fileName);
                byte data[] = new byte[1024];

                int count = 0;
                long total = 0;


                while ((count=is.read(data)) != -1) {
                    total += count;
                    int progress_temp = (int)(total * 100) / lenghtOfFile;
                    if((progress_temp % 10) == 0 && (progress != progress_temp)){
                        progress = progress_temp;
                        Log.v(TAG, "total = "+progress);    
                    }
                    fos.write(data, 0, count);
                }
                is.close();
                fos.close();
                Log.v(TAG, "downloading finished");
            }
        }catch(Exception e){
            Log.v(TAG, "exception in downloadData");
            e.printStackTrace();
        }

Upvotes: 0

Related Questions