Android Webview应用程序无法下载成功

时间:2014-09-04 11:01:40

标签: java android webview

我的Android应用有一个Webview可以访问我的网站。我注意到在服务器中,当应用程序下载文件时,使用的带宽小于其他设备或浏览器下载的带宽。

在onDownloadStart方法中,我调用了一个AsyncTask类:

protected String doInBackground(String... sUrl) {
    try {
        URL url = new URL(sUrl[0]); 

        //Getting directory to store the file

        //Connection handler
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();

        //Obtaining filename

        File outputFile = new File(directory, filename);
        InputStream input   = new BufferedInputStream(connection.getInputStream());
        OutputStream output = new FileOutputStream(outputFile);

        byte buffer[] = new byte[1024];
        int bufferLength = 0;
        int total = 0;
        while ((bufferLength=input.read(buffer))!=-1) {
            total += bufferLength;
            output.write(buffer, 0, bufferLength);
        }             
        connection.disconnect();
        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
         e.printStackTrace();
    }
    return null;
}

下载的文件是空的,尽管它们的文件名和格式是正确的,我从服务器收到HTTP 200消息;也执行不进入while循环。我试图改变缓冲区大小,问题没有解决。

0 个答案:

没有答案