Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android URLConnection类的Get content length方法(getContentLength())返回负1(-1)_Android_Http_Cloudboost - Fatal编程技术网

Android URLConnection类的Get content length方法(getContentLength())返回负1(-1)

Android URLConnection类的Get content length方法(getContentLength())返回负1(-1),android,http,cloudboost,Android,Http,Cloudboost,下面是我从CloudBoost数据库下载PDF文件的AsyncTask。代码工作并下载文件;但是,进度条更新不起作用,因为返回的文件长度是-1。有人能告诉我怎么处理这件事吗 顺便说一下,加载.setProgress(progress[0])中的行,在嵌套此AsyncTask类的类的顶部初始化进度更新方法 class DownloadPdfFromInternet extends AsyncTask<String, Integer, String> { @Override

下面是我从
CloudBoost
数据库下载PDF文件的
AsyncTask
。代码工作并下载文件;但是,进度条更新不起作用,因为返回的文件长度是
-1
。有人能告诉我怎么处理这件事吗

顺便说一下,加载.setProgress(progress[0])中的行,在嵌套此
AsyncTask
类的类的顶部初始化进度更新方法

class DownloadPdfFromInternet extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... strings) {
        int count;
        try {
            URL url = new URL(strings[0]);
            URLConnection connection = url.openConnection();
            connection.connect();
            // get length of file
            int lengthOfFile = connection.getContentLength();
            Log.d("dozer74", "LengthOf File: " + lengthOfFile);

            InputStream input = new BufferedInputStream(url.openStream(), 10 * 1024);
            OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/" + pubName);
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress((int) (total * 100 / lengthOfFile));
                // write data to file
                output.write(data, 0, count);
            }
            output.flush();
            output.close();
            input.close();
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
        return null;
    }

    protected void onProgressUpdate(Integer... progress) {
        loading.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(String url) {
        loading.dismiss();
        openPdfFile();
    }

}
类下载PdfFromInternet扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的字符串背景(字符串…字符串){
整数计数;
试一试{
URL=新URL(字符串[0]);
URLConnection=url.openConnection();
connection.connect();
//获取文件的长度
int lengthOfFile=connection.getContentLength();
Log.d(“dozer74”,“LengthOf文件:“+lengthOfFile”);
InputStream输入=新的BufferedInputStream(url.openStream(),10*1024);
OutputStream output=新文件OutputStream(Environment.getExternalStorageDirectory().getPath()+“/”+pubName);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
出版进度((整数)(总计*100/长办公室));
//将数据写入文件
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
受保护的void onProgressUpdate(整数…进度){
正在加载.setProgress(progress[0]);
}
@凌驾
受保护的void onPostExecute(字符串url){
loading.dispose();
openPdfFile();
}
}

找到相关答案,例如。在StackOverflow上还有其他类似的问题和答案。。仍然在处理
URLConnection
类?听说过改装吗