Libcurl & Log4cplus 移植和使用 以及 Jsoncpp 简单使用

#PS:要转载请注明出处,本人版权所有

#PS:这个只是 《 我自己 》理解,如果和你的

#原则相冲突,请谅解,勿喷

Libcurl篇(curl-7.55.1.tar.gz)

移植Libcurl

我首先看了一下其目录结构,里面存在两套编译结构,一个是依赖于CMake,一个是依赖于Autoconf。
这里使用的是Autoconf。

    ./configure --prefix=你的安装绝对路径 --host=arm-linux-gnueabi --target=arm-linux-gnueabi CXX=arm-linux-gnueabi-g++ CC=arm-linux-gnueabi-gcc
    make -j16
    make install

说明:–prefix 指定安装路径, –host以及–target指定目标架构, CC和CXX变量指定了工具链

使用Libcurl

curl_global_init
curl_easy_init
curl_easy_setopt
//CURLOPT_HTTPHEADER 设置头信息
//CURLOPT_URL 设置url信息
//CURLOPT_POST 设置本次http请求方式是否为post
//CURLOPT_POSTFIELDS 设置post 数据域
//CURLOPT_WRITEFUNCTION 设置http返回信息的回调函数
//CURLOPT_WRITEDATA 设置回调函数中,userdata的数据内存
curl_easy_perform
curl_easy_cleanup
curl_global_cleanup
int yUpgrade::ReportUpgradeStatus(const std::string & str){

   // InitLibCurl(ycurl);

    if ( CURLE_OK != (res = curl_global_init(CURL_GLOBAL_ALL)) ){
    std::cout<<"curl_global_init call  failed "<<std::endl;
    return -1;
    } 
    if ( !( ycurl = curl_easy_init() ) ){
    std::cout<<"curl_easy_init call  failed "<<std::endl;
    return -1;
    }

    struct curl_slist *headers = NULL;
    headers=curl_slist_append(headers, "Content-Type:application/json");
    headers=curl_slist_append(headers, "Accept:application/json");
    headers = curl_slist_append(headers, "charset:utf-8");

    curl_easy_setopt(ycurl, CURLOPT_HTTPHEADER, headers);
    //headers = curl_slist_append(headers, "Accept: Agent-007");
    //curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    curl_easy_setopt(ycurl, CURLOPT_URL, REPORTUPGRADESTATUS_URL);
     //curl_easy_setopt(curl,  CURLOPT_CUSTOMREQUEST, "POST");//自定义请求方式
    curl_easy_setopt(ycurl, CURLOPT_POST, 1);//设置为非0表示本次操作为POS

    //curl_easy_setopt(ycurl, CURLOPT_POSTFIELDSIZE, str.size());//设置上传json串长度,这个设置可以忽略
    curl_easy_setopt(ycurl, CURLOPT_POSTFIELDS, str.c_str()); 

    curl_easy_setopt(ycurl, CURLOPT_WRITEFUNCTION, write_callback);//设置回调函数
    curl_easy_setopt(ycurl, CURLOPT_WRITEDATA, RecCharBuf);//设置写数据

    if ( CURLE_OK !=  (res = curl_easy_perform(ycurl)) ){

        CleanUpLibCurl(ycurl);
        curl_slist_free_all(headers);
        std::cout<<"curl_easy_perform call  failed,Res= "<<res<<std::endl;
        return -1;
    }


    CleanUpLibCurl(ycurl);
    curl_slist_free_all(headers);
    return 0;
}

Log4cplus篇(log4cplus-1.2.1-rc2.7z)

移植Log4cplus

./configure --host=arm-linux-gnueabi --target=arm-linux-gnueabi CC=arm-hisiv400-linux-gcc CXX=arm-hisiv400-linux-g++
make -j8
make install

Log4cplus的使用(网上去找教程就可以了,一大堆,推荐使用加载配置文件的方式,不要在程序中配置)

1 PropertyConfigurator 类来加载配置文件
2 然后正常使用就可以了

Jsoncpp篇(1.8.3)

Jsoncpp的简单使用(注意,Jsoncpp更新了,有些接口不推荐了,但是可以用,网上大部分的教程都是旧版的,这里我会给出新的和旧的的使用).这里只有读的。写的类似。

    std::string G4FileUrl;
    // Json::CharReader *tmp ;
    // Json::CharReaderBuilder * ptmp =  new Json::CharReaderBuilder();
    // tmp = ptmp->newCharReader();
    //tmp->parse(upgrade.RecStr,root1)
    Json::Reader UpGradeInfo;
    Json::Value root1;
    UpGradeInfo.parse(upgrade.RecStr,root1)
    G4FileUrl = root1["fileInfo"]["fileUrl"].asString();
    /*例子json结构
{
"fileInfo":{
"id":{"timestamp":1508143624,"machineIdentifier":439847,"processIdentifier":524,
"counter":15004360},
"fileUrl":"http://xxxxxxxxx/group1/M00/00/35/wKgfdVnkcgiAVnsqAAHEhu0PItU720.jpg",
"fileName":"TX.jpg",
"fileType":4,
"cameraId":0,
"addedDate":"Oct 16, 2017 4:47:04 PM"}
    }
}
*/

说明,如果使用旧版接口,gcc会报warning: ‘Reader’ is deprecated: Use CharReader and CharReaderBuilder instead [-Wdeprecated-declarations],msvc (// MSVC 2008 以上 )会报error。
#PS:请尊重原创,不喜勿喷

#PS:要转载请注明出处,本人版权所有.

有问题请留言,看到后我会第一时间回复

猜你喜欢

转载自blog.csdn.net/u011728480/article/details/78582463
今日推荐