Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

预加载进不去啊 #296

Open
Serrywang opened this issue Dec 29, 2020 · 4 comments
Open

预加载进不去啊 #296

Serrywang opened this issue Dec 29, 2020 · 4 comments

Comments

@Serrywang
Copy link

private void start() {
L.i("开始预加载:" + mPosition);
Log.d(TAG, "start: >>>"+"开始预加载aaa");
Toast.makeText(MyApplication.getAppInstance(), "开始预加载", Toast.LENGTH_SHORT).show();
HttpURLConnection connection = null;
try {
//获取HttpProxyCacheServer的代理地址
String proxyUrl = mCacheServer.getProxyUrl(mRawUrl);
Log.d(TAG, "start: >>>"+"开始预加载proxyUrl");
Log.d(TAG, "start: >>>"+proxyUrl);
URL url = new URL(proxyUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
InputStream in = new BufferedInputStream(connection.getInputStream());
Log.d(TAG, "start: >>>"+"进行预加载");
Toast.makeText(MyApplication.getAppInstance(), "预加载。。", Toast.LENGTH_SHORT).show();
int length;
int read = -1;
byte[] bytes = new byte[8 * 1024];
//读到bytes数组里面去
while ((length = in.read(bytes)) != -1) {
read += length;
//预加载完成或者取消预加载
if (mIsCanceled || read >= PreloadManager.PRELOAD_LENGTH) {
L.i("结束预加载:" + mPosition);
Log.d(TAG, "start: >>>"+"结束预加载");
Toast.makeText(MyApplication.getAppInstance(), "结束预加载", Toast.LENGTH_SHORT).show();
break;
}
}
if (read == -1) { //这种情况一般是预加载出错了,删掉缓存
L.i("预加载失败:" + mPosition);
Toast.makeText(MyApplication.getAppInstance(), "预加载失败", Toast.LENGTH_SHORT).show();
File cacheFile = mCacheServer.getCacheFile(mRawUrl);
if (cacheFile.exists()) {
cacheFile.delete();
}
}
} catch (Exception e) {
L.i("异常结束预加载:" + mPosition);
Log.d(TAG, "start: >>>"+"异常");
Toast.makeText(MyApplication.getAppInstance(), "异常结束预加载", Toast.LENGTH_SHORT).show();
} finally {
if (connection != null) {
connection.disconnect();
Log.d(TAG, "start: >>>"+"异常");
}
}
}
这个方法走不完

@fza3230200
Copy link

我也遇到了相同的问题, 就是在下载过程中,有时候突然就不走了,不知道为啥...你遇到过吗

@fza3230200
Copy link

而且感觉他的缓存速度好慢啊

@jackli12
Copy link

private void start() { L.i("开始预加载:" + mPosition); Log.d(TAG, "start: >>>"+"开始预加载aaa"); Toast.makeText(MyApplication.getAppInstance(), "开始预加载", Toast.LENGTH_SHORT).show(); HttpURLConnection connection = null; try { //获取HttpProxyCacheServer的代理地址 String proxyUrl = mCacheServer.getProxyUrl(mRawUrl); Log.d(TAG, "start: >>>"+"开始预加载proxyUrl"); Log.d(TAG, "start: >>>"+proxyUrl); URL url = new URL(proxyUrl); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(3000); connection.setReadTimeout(3000); InputStream in = new BufferedInputStream(connection.getInputStream()); Log.d(TAG, "start: >>>"+"进行预加载"); Toast.makeText(MyApplication.getAppInstance(), "预加载。。", Toast.LENGTH_SHORT).show(); int length; int read = -1; byte[] bytes = new byte[8 * 1024]; //读到bytes数组里面去 while ((length = in.read(bytes)) != -1) { read += length; //预加载完成或者取消预加载 if (mIsCanceled || read >= PreloadManager.PRELOAD_LENGTH) { L.i("结束预加载:" + mPosition); Log.d(TAG, "start: >>>"+"结束预加载"); Toast.makeText(MyApplication.getAppInstance(), "结束预加载", Toast.LENGTH_SHORT).show(); break; } } if (read == -1) { //这种情况一般是预加载出错了,删掉缓存 L.i("预加载失败:" + mPosition); Toast.makeText(MyApplication.getAppInstance(), "预加载失败", Toast.LENGTH_SHORT).show(); File cacheFile = mCacheServer.getCacheFile(mRawUrl); if (cacheFile.exists()) { cacheFile.delete(); } } } catch (Exception e) { L.i("异常结束预加载:" + mPosition); Log.d(TAG, "start: >>>"+"异常"); Toast.makeText(MyApplication.getAppInstance(), "异常结束预加载", Toast.LENGTH_SHORT).show(); } finally { if (connection != null) { connection.disconnect(); Log.d(TAG, "start: >>>"+"异常"); } } } 这个方法走不完

走不完,没有异常信息么,网络错误或者是io读取错误之类的

@chenshi011
Copy link

request需要添加range合适一点,前提是服务器本身支持range。connection.setRequestProperty("Range", String.format("bytes=%d-%d", 0, PreloadManager.PRELOAD_LENGTH));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants