English 中文(简体)
下载流出错误的意外结束? [复制]
原标题:unexpected end of stream error on download? [duplicate]
  • 时间:2011-11-20 16:09:50
  •  标签:
  • android
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
Android:“Unexpected end of stream” exception downloading large files

I mding a file of approx. 5MB, using HttpURLConnection but part way during thedownlod i have an “unexpected end of stream” mis at this line of my Code:

                     while ((count = input.read(data)) > 0) {

Here is the LOG:

11-20 16:05:55.749: ERROR/PRINTSTACK(3425): STACK:unexpected end of stream
11-20 16:05:55.749: WARN/System.err(3425): java.io.IOException: unexpected end of stream
11-20 16:05:55.749: WARN/System.err(3425):     at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream.read(FixedLengthInputStream.java:47)
11-20 16:05:55.749: WARN/System.err(3425):     at java.io.BufferedInputStream.read(BufferedInputStream.java:319)
11-20 16:05:55.749: WARN/System.err(3425):     at java.io.FilterInputStream.read(FilterInputStream.java:133)
11-20 16:05:55.759: WARN/System.err(3425):     at com.conjure.skiproj.DownloadService$Job.process(DownloadService.java:265)
11-20 16:05:55.759: WARN/System.err(3425):     at com.conjure.skiproj.DownloadService$1.run(DownloadService.java:193)
11-20 16:05:55.759: WARN/System.err(3425):     at java.lang.Thread.run(Thread.java:1019)

帮助!

EDIT: 更注重制定投入的法规:

    HttpURLConnection conexion = (HttpURLConnection)url.openConnection();
                        conexion.setRequestMethod("GET");
                        conexion.setReadTimeout(20000);
                        conexion.connect();
                        File file = new File(root.getAbsolutePath()+"/", fileName);

                             int lenghtOfFile = conexion.getContentLength();

                             InputStream input = new BufferedInputStream(url.openStream());

                             OutputStream output = new FileOutputStream(file);

                             byte data[] = new byte[8192];
...........................

EDIT 2. 在以下线上下载25%的新错误索引OutOfBoundsException:

  output.write(data, 0, count);

缩略语

11-20 17:47:02.311: ERROR/totaltotal(303): 24
11-20 17:47:02.311: INFO/System.out(303): countcountcount:4332
11-20 17:47:02.330: ERROR/totaltotal(303): 24
11-20 17:47:02.330: INFO/System.out(303): countcountcount:2904
11-20 17:47:02.330: ERROR/totaltotal(303): 25
11-20 17:47:02.330: INFO/System.out(303): countcountcount:1452
11-20 17:47:02.330: ERROR/totaltotal(303): 25
11-20 17:47:02.650: INFO/System.out(303): countcountcount:4356
11-20 17:47:02.650: ERROR/totaltotal(303): 25
11-20 17:47:02.650: INFO/System.out(303): countcountcount:-1
11-20 17:47:02.660: ERROR/totaltotal(303): 25
11-20 17:47:02.892: DEBUG/dalvikvm(303): GC_FOR_MALLOC freed 10770 objects / 490896 bytes in 143ms
11-20 17:47:03.060: ERROR/PRINTSTACK(303): STACK:Arguments out of bounds
11-20 17:47:03.060: WARN/System.err(303): java.lang.IndexOutOfBoundsException: Arguments out of bounds
11-20 17:47:03.070: WARN/System.err(303):     at java.io.FileOutputStream.write(FileOutputStream.java:288)
11-20 17:47:03.080: WARN/System.err(303):     at com.conjure.skiproj.DownloadService$Job.process(DownloadService.java:275)
11-20 17:47:03.080: WARN/System.err(303):     at com.conjure.skiproj.DownloadService$1.run(DownloadService.java:191)
11-20 17:47:03.080: WARN/System.err(303):     at java.lang.Thread.run(Thread.java:1096)

EDIT 3: I traced the error back to FixedLengthInputStream class and then further back to AbstractHttpInputStream class in where there is this method:

/**
             * Calls abort on the cache entry and disconnects the socket. This
             * should be invoked when the connection is closed unexpectedly to
             * invalidate the cache entry and to prevent the HTTP connection from
             * being reused. HTTP messages are sent in serial so whenever a message
             * cannot be read to completion, subsequent messages cannot be read
             * either and the connection must be discarded.
             *
             * <p>An earlier implementation skipped the remaining bytes, but this
             * requires that the entire transfer be completed. If the intention was
             * to cancel the transfer, closing the connection is the only solution.
             */
            protected final void unexpectedEndOfInput() {
                if (cacheRequest != null) {
                    cacheRequest.abort();
                }
                httpURLConnection.releaseSocket(false);
            }

因此,似乎在Http电文出现错误时,整个下载流都被取消。

问题回答

固定投入Stream推翻了这一例外,因为预期的用药数量(通常在答复的内容部分)大于答复中的实际数据。 内容透镜头正确。 (如果你重新提供自己对内容长度的价值,确保它正确无误)。)

It would help to see your code that sets up the input stream.

只要我理解,如果存在网络问题,就可以算出0。 Trys

while ((count = input.read(data)) != -1) {
    if (count != 0) {
        ...
    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签