姜饼上的Android Inputstream.read问题(下载时)

姜饼上的Android Inputstream.read问题(下载时),android,download,inputstream,Android,Download,Inputstream,我在这里没有发现这样的问题 昨天我终于在NexusOne上安装了姜饼2.3.4。当我再次打开应用程序(基本上是将XML提要加载到ListView中)时,它在下载时卡住了 似乎InputStream;->流读取(缓冲区);完成后不再返回-1 这里的代码几乎相同 这是我的密码: public InputStream getInputStreamFromURL(String urlString, DownloadProgressCallback callback) throws IOExce

我在这里没有发现这样的问题

昨天我终于在NexusOne上安装了姜饼2.3.4。当我再次打开应用程序(基本上是将XML提要加载到ListView中)时,它在下载时卡住了

似乎InputStream;->流读取(缓冲区);完成后不再返回-1

这里的代码几乎相同

这是我的密码:

public InputStream getInputStreamFromURL(String urlString, DownloadProgressCallback callback) 
    throws IOException, IllegalArgumentException
    {
        InputStream in = null;

        conn = (HttpURLConnection) new URL(urlString).openConnection();
        fileSize = conn.getContentLength();
        out = new ByteArrayOutputStream((int) fileSize);
        conn.connect();

        stream = conn.getInputStream();
        // loop with step 1kb
        while (status == DOWNLOADING) {
            byte buffer[];

            if (fileSize - downloaded > MAX_BUFFER_SIZE) {
                buffer = new byte[MAX_BUFFER_SIZE];
            } else {
                buffer = new byte[(int) (fileSize - downloaded)];
            }
            int read = stream.read(buffer);

            if (read == -1) {
                break;
            }
            // writing to buffer
            out.write(buffer, 0, read);
            downloaded += read;
            // update progress bar
            callback.progressUpdate((int) ((downloaded / fileSize) * 100));
        }// end of while

        if (status == DOWNLOADING) {
            status = COMPLETE;
        }
        in= (InputStream) new ByteArrayInputStream(out.toByteArray());
        // end of class DownloadImageTask()
        return in;     
    }
问题基本上是当下载完成时,stream.read(buffer)返回0而不是-1。当我改变

if (read == -1) {
            break;
        }
到0或

if (fileSize == downloaded) {
            break;
        }
我在MainActivity上获得ParseExceptions(ExpatParser)。 在2.2上,它运行得非常完美

我已经清除了应用缓存并尝试了其他一些方法,但我现在真的被卡住了

我希望有人能帮助我。:)

更新:

太棒了,你就是那个人,纪尧姆。:) 非常感谢,这救了我一个晚上!:)

您的代码满足了我的需要:

public InputStream getStreamFromURL(String urlString, DownloadProgressCallback callback){
    // initialize some timeouts
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters,3000);

    // create the connection
    URL url;
    try {
        url = new URL(urlString);
        URLConnection connection = url.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
     // connection accepted
        if(httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            int size = connection.getContentLength();

            int index = 0;
            int current = 0;



                InputStream input = connection.getInputStream();
                BufferedInputStream buffer = new BufferedInputStream(input);
                byte[] bBuffer = new byte[1024];
                out = new ByteArrayOutputStream((int) size);

                while((current = buffer.read(bBuffer)) != -1) {

                    out.write(bBuffer, 0, current);
                    index += current;
                    callback.progressUpdate((index/size)*100);
                }
                out.close();

        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return (InputStream) new ByteArrayInputStream(out.toByteArray());

}

此代码适用于我的2.3.4 Nexus One:

try {
    // initialize some timeouts
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);

    // create the connection
    URL url = new URL(toDownload);
    URLConnection connection = url.openConnection();
    HttpURLConnection httpConnection = (HttpURLConnection) connection;

    // connection accepted
    if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
        try {
            file = new File(destination);
            // delete the file if exists
            file.delete();
        } catch (Exception e) {
            // nothing
        }

        int size = connection.getContentLength();

        int index = 0;
        int current = 0;

        try {
            file = new File(destination);
            file.delete();
            FileOutputStream output = new FileOutputStream(file);
            InputStream input = connection.getInputStream();
            BufferedInputStream buffer = new BufferedInputStream(input);
            byte[] bBuffer = new byte[10240];

            while ((current = buffer.read(bBuffer)) != -1) {
                if (isCancelled()) {
                    file.delete();
                    break;
                }

                try {
                    output.write(bBuffer, 0, current);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                index += current;
                publishProgress(index / (size / 100));
            }
            output.close();
        } catch (SecurityException se) {
            se.printStackTrace();
            return 1;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 2;
        }

        return 0;
    }

    // connection refused
    return 2;
} catch (IOException e) {
    return 2;
}