请教一下关于android数据更新问题,我有段代码,关于网络下载信息的

2025-12-17 09:19:12
推荐回答(1个)
回答1:

//使用Handler来动态更新进度
public class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == MSG_WHAT){
                progressView.setText(context.getString(R.string.download_progress)+msg.obj.toString()+"%");
            }
            super.handleMessage(msg);
        }
    }
//Connectin里面的下载方法
public synchronized int loadNewApp(String localPath, String url) {
        String path = session.getCacheDir();
        if(path == null){
            return Constant.DOWNLOAD_FAILTURE;
        }
        File file = new File(session.getCacheDir()+localPath+Constant.APP_END);
        if(file.exists()){
            file.delete();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            LogUtil.e(e.getMessage(), e);
        }
        HttpGet req;
        URI uri;
        try {
            uri = new URI(url);
            req = new HttpGet(uri);
        } catch (URISyntaxException e1) {
            //LogUtil.error(Constant.FILEDB_LOG, "URLEncoder: " + url);
            req = new HttpGet(encodeUrl(url));
        }
        HttpResponse response;
        try {
            response = execute(req);
            if (response == null) {
                return Constant.DOWNLOAD_FAILTURE;
            }
                                
            if (response.getStatusLine() == null) {
                return Constant.DOWNLOAD_FAILTURE;
            }
            long curPosition = 0, contentLength = 0;
            contentLength= Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
            LogUtil.d("contentLength"+contentLength);
            int statusCode = response.getStatusLine().getStatusCode();
            LogUtil.d("statusCode"+statusCode);
            if (statusCode == HttpURLConnection.HTTP_PARTIAL || statusCode == HttpURLConnection.HTTP_OK) {
                HttpEntity entity = response.getEntity();
                InputStream is = entity.getContent();
                                    
                FileOutputStream accessFile = new FileOutputStream(file);
                byte buf[] = new byte[8*1024];
                int numread = -1;
                while ((numread = is.read(buf)) != -1 && !stopDownLoadFile) {
                    accessFile.write(buf, 0, numread);
                    curPosition += numread;
                    float f = ((float)curPosition)/((float)contentLength);
                    Message msg = CheckUpdate.handler.obtainMessage();
                    msg.what = CheckUpdate.MSG_WHAT;
                    msg.obj = Float.toString((int)(f*100));
                    msg.sendToTarget();
                }
                is.close();
                accessFile.close();
                if(stopDownLoadFile){
                    return Constant.DOWNLOAD_FAILTURE;
                }else{
                    return Constant.DOWNLOAD_SUCCESS;
                }
            }
        } catch (IOException e) {
            LogUtil.e(e.getMessage(), e);
        }
        return Constant.DOWNLOAD_FAILTURE;
    }

如果有不明白的请Hi我