0

我确实读过类似的问题,但对我没有任何作用。问题是方法 getProgress() 返回非常大的值(例如 16300 )。我做错了什么?

看我的代码

 public void download()
{
    RandomAccessFile file = null;
    InputStream inputStream = null;
    HttpURLConnection connection = null;
    try
    {
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();
        String connectLength = connection.getHeaderField("Content-Length");

        if(connectLength == null || Integer.parseInt(connectLength) < 1 || connectLength.equals(""))
            error();

        size = Long.parseLong(connectLength);
        file = new RandomAccessFile(filePath,"rw");
        inputStream = new BufferedInputStream(url.openStream());
        int read ;

        while((read = inputStream.read(buffer)) > 0 && status == States.DOWNLOADING)
        {
            file.write(buffer,0,read);
            downloadedS += read;
        }
    }
}
 public float getProgress()
    { 
        return ((float)downloadedS/size)*100;
    }
4

1 回答 1

1

AsyncTask 允许正确和轻松地使用 UI 线程。此类允许在 UI 线程上执行后台操作并发布结果,而无需操作线程和/或处理程序。异步任务

于 2013-10-08T15:55:49.677 回答