三 cocos2dx 安卓安装包整包更新

    cocos2d-c++用了大概一年多了,以前一直搞单机,现在要求搞更新,c++安卓端更新比较麻烦,一开始想做个热更新,但是一直找不到方法,最后决定只能退而求其次,做个整包更新。

    1   服务器必须要有的,服务端程序倒是不必有,在服务端我们只需要放两个东西,第一要在服务端放个配置文件来标识app最新版本就ok。第二要在服务器放个我们的最新版本的app安装包。截个图:


config.xml是配置版本信息的,里面是这样滴(根据自己的需求写):



haha.apk 就是你最新的app安装包了,webconfig不用管。


2    客户端在程序启动的时候要做3个事情,第一个下载服务器版本信息文件,写入本地文件。第二个 将写入本地的配置文件内的版本信息与当前版本信息比对,若一致则不更新,若不一致就更新。第三个,下载新的app安装包,可以用openurl直接调用安卓浏览器来下载,如果不闲麻烦可以自己下载下来写入sd卡。  代码:

void cupdate::updateVersion()
{
    httprequst(nullptr);
}
void cupdate::httprequst(cocos2d::Object *sender)
{
    HttpRequest* request = new HttpRequest();//创建request对象,这里new出来的对象不能使用autorelease(),原因后述
    request->setUrl("http://你自己的服务器地址");//设置url
    request->setRequestType(HttpRequest::Type::GET);//设置请求方式
    request->setResponseCallback(this, CC_CALLFUNCND_SELECTOR(cupdate::onHttpRequestCompleted));//这是回调对象和回调函数
    request->setTag("GET test1");//设置用户标识,可以通过response获取
    HttpClient::getInstance()->send(request);//使用CCHttpClient共享实例来发送request
    request->release();//调用release()
}
void cupdate::onHttpRequestCompleted(cocos2d::Node *sender, void *data)
{
    //接收数据
    HttpResponse *response = (HttpResponse*)data;
    if (!response)
    {
        return;
    }
    // 获取对应request的字符串标识
    if (0 != strlen(response->getHttpRequest()->getTag()))
    {
        CCLog("%s completed", response->getHttpRequest()->getTag());
    }
    //获取返回代码,比如200、404等
    int statusCode = response->getResponseCode();
    char statusString[64] = {};
    sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
    CCLog("response code: %d", statusCode);
    
    if (!response->isSucceed())
    {
        CCLog("response failed");
        CCLog("error buffer: %s", response->getErrorBuffer());//可以调用getErrorBuffer()来获取错误原因
        return;
    }
    // dump data
    std::vector<char> *buffer = response->getResponseData();//用来获取接收到的数据
    //处理数据
    std::string path= FileUtils::getInstance()->getWritablePath();
    std::string fullPath =  path + "sever_config.xml";
    FILE* fp = fopen(fullPath.c_str(), "wb");
    log("将文件写入本地 %s",fullPath.c_str());
    unsigned char bf;
    for (unsigned int i  = 0; i < buffer->size(); i++) {
        
        bf = buffer->at(i);
        fwrite(&bf, 1, 1, fp);
        
    }
    fclose(fp);
    
    //解析文件
    ssize_t size;
    
   
    const char *mode="r";
    unsigned char *pFileContent =FileUtils::getInstance()->getFileData(FileUtils::getInstance()->getWritablePath()+"sever_config.xml", mode, &size);
    TiXmlDocument doc;
    const char *p=(const char *)pFileContent;
    doc.Parse(p, 0, TIXML_ENCODING_UTF8);
    TiXmlElement* config = doc.FirstChildElement();
    float vertion=atof(config->Attribute("version"));//服务器版本
    log("%f",vertion);
    
    //解析本地版本
//    FILE *fpp=fopen((FileUtils::getInstance()->getWritablePath()+"config.xml").c_str(), "rb");
//    std::string file_path;
//    
//    if(fpp==nullptr)
//    {
//        file_path="config.xml";
//    }
//    else
//    {
//        file_path="config.xml";
//        fclose(fpp);
//    }
    std::string file_path="config.xml";
    ssize_t size_local;
    const char *mode_local="r";
    unsigned char *pFileContent_local =FileUtils::getInstance()->getFileData(file_path, mode_local, &size_local);
    TiXmlDocument doc_local;
    const char *p_local=(const char *)pFileContent_local;
    doc_local.Parse(p_local, 0, TIXML_ENCODING_UTF8);
    TiXmlElement* config_local = doc_local.FirstChildElement();
    float vertion_local=atof(config_local->Attribute("version"));
    log("%f",vertion_local);
    if(vertion_local!=vertion)
    {
        log("有更新,下载新的apk!!!");
       
        //Director::getInstance()->getScheduler()->schedule(schedule_selector(cupdate::qidongDownLoad), this, 0, 0, 0.1, false);
        Application::getInstance()->openURL("http://你自己的服务器地址/haha.apk");
    }
    else
    {
        log("木有更新");
    }
   
}

这样就可以实现纯c++写的app整包更新,不用去应用平台发布新应用了。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值