0

I'm downloading different medias files from my http server; mp3, jpg/png/ and html.
Everything worked fine when I used the now deprecated HttpClient.
I decided to use the HttpURLConnection.

But I encounter a problem with text files(html).
read() blocks on small html files, maybe waiting for a EOF or I don't know what, during few seconds and exits with the Exception "unexpected end of stream".

My code is:

URL url = new URL(urlString);

postParams = String.format("registration=%s&"....);

urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setDoOutput(true); // It's a POST request which replies sending a file
urlConnection.setChunkedStreamingMode(0);
if (fileName.contains(".htm")) { // Tried this to see...
    urlConnection.setRequestProperty("Accept-Charset", "UTF-8");
    urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + "UTF-8");
}

OutputStream output = urlConnection.getOutputStream();
output.write(postParams.getBytes("UTF-8"));

is = new BufferedInputStream(urlConnection.getInputStream());   

/* Get information from the HttpURLConnection automatically fires the request
 * http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests
 */
int status = urlConnection.getResponseCode();   

if (status == 200) {

    int len, size = 0;
    byte[] buf = new byte[128 * 1024];

    BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));

    try {
        while ((len = is.read(buf, 0, buf.length)) > 0) { 
            os.write(buf, 0, len);
            size += len;
        }
        os.flush();
    } catch (IOException e) {
        Log.d(Constants.APP_TAG, "IOException." + e); // HTML files end here after few seconds
    } finally {
        nbFilesDownloaded++;
        is.close();
        os.close();

        file.setReadable(true, false);
        file.setWritable(true, false);
    }
}

Any idea to explain why it cannot normally exit from read()??

EDIT: I verified that for the html files which cause this exception, my webserver doesn't include Content-Length in the header. Can it be the cause of the problem?

fralbo
  • 2,304
  • 2
  • 33
  • 67

1 Answers1

0

Try to use okhttp http://square.github.io/okhttp/. I have downloaded the files correctly. I hope to help.

Paul.

ArtPaul
  • 1
  • 1
  • Ok Paul, thanks, I'll have a look at it. Anyway I'd like to understand what is the problem with HttpURLConnection... – fralbo Mar 16 '15 at 21:48
  • Same problem with OkHttp! I suspect the webserver – fralbo Mar 17 '15 at 15:11
  • Very similar to this. Try to download a file from another server. – ArtPaul Mar 17 '15 at 19:54
  • I'll try Paul but I would surprised that something changed on OVH Apache configuration and I verified until return() that my webservice header contains Content-Length. I wonder if something in android API could remove this header field... – fralbo Mar 17 '15 at 20:51
  • No other idea about this problem? – fralbo Mar 20 '15 at 18:26
  • you can find additionnal infos and url access here. https://code.google.com/p/android/issues/detail?id=160705 – fralbo Mar 24 '15 at 18:39