ROS语音交互(四)接入图灵语义理解

发布时间:2017-5-29 23:44:33 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"ROS语音交互(四)接入图灵语义理解 ",主要涉及到ROS语音交互(四)接入图灵语义理解 方面的内容,对于ROS语音交互(四)接入图灵语义理解 感兴趣的同学可以参考一下。

首先程序中会用到Json,curl

安装相应的库

$ sudo apt-get install libcurl3 libcurl4-openssl-dev
$ sudo apt-get install libjsoncpp0 libjsoncpp-dev

关机重启

参考

C++ 用libcurl库进行http通讯网络编程                   http://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html

 使用curl库,以post方式向服务器发送json数据  http://blog.csdn.net/dgyanyong/article/details/14166217

libcurl使用程序总结                                                   http://www.programgo.com/article/71711741757/

在上节 tts_voice/src 中添加tuling_arv.cpp

/* *  tuling_arv.cpp *  tuling_arv_node*/#include<ros/ros.h>#include<std_msgs/String.h>#include<iostream>#include<sstream>#include<jsoncpp/json/json.h>#include<curl/curl.h>#include<string>#include<exception>using namespace std;int flag = 0;string result; int writer(char *data, size_t size, size_t nmemb, string *writerData){    unsigned long sizes = size * nmemb;    if (writerData == NULL)        return -1;    writerData->append(data, sizes);    return sizes;}/**   解析图灵服务器返回的Json string*/int parseJsonResonse(string input){   Json::Value root;   Json::Reader reader;   bool parsingSuccessful = reader.parse(input, root);   if(!parsingSuccessful)   {       std::cout<<"!!! Failed to parse the response data"<< std::endl;        return -1;   }   const Json::Value code = root["code"];   const Json::Value text = root["text"];   result = text.asString();   flag = 1;   std::cout<< "response code:" << code << std::endl;   std::cout<< "response text:" << result << std::endl;   return 0;}/**   将input字腹发送到图灵服务器接受json string*/int HttpPostRequest(string input){    string buffer;    std::string strJson = "{";    strJson += "\"key\" : \"1bf4cfd53fea4ee09f327822c53ad20c\","; //双引号前加/防转仪    strJson += "\"info\" : ";    strJson += "\"";    strJson += input;    strJson += "\"";    strJson += "}";    std::cout<<"post json string: " << strJson << std::endl;     try    {        CURL *pCurl = NULL;        CURLcode res;        // In windows, this will init the winsock stuff        curl_global_init(CURL_GLOBAL_ALL);        // get a curl handle        pCurl = curl_easy_init();        if (NULL != pCurl)        {            // 设置超时时间为10秒            curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 10);            // First set the URL that is about to receive our POST.            // This URL can just as well be a            // https:// URL if that is what should receive the data.            curl_easy_setopt(pCurl, CURLOPT_URL, "http://www.tuling123.com/openapi/api");            //curl_easy_setopt(pCurl, CURLOPT_URL, "http://192.168.0.2/posttest.cgi");            // 设置http发送的内容类型为JSON            curl_slist *plist = curl_slist_append(NULL,                "Content-Type:application/json;charset=UTF-8");            curl_easy_setopt(pCurl, CURLOPT_HTTPHEADER, plist);            // 设置要POST的JSON数据            curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, strJson.c_str());            curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, writer);            curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &buffer);            // Perform the request, res will get the return code            res = curl_easy_perform(pCurl);            // Check for errors            if (res != CURLE_OK)            {                printf("curl_easy_perform() failed:%s\n", curl_easy_strerror(res));            }            // always cleanup            curl_easy_cleanup(pCurl);        }        curl_global_cleanup();    }    catch (std::exception &ex)    {        printf("curl exception %s.\n", ex.what());    }    if(buffer.empty())    {      std::cout<< "!!! ERROR The Tuling sever response NULL" << std::endl;    }    else    {        parseJsonResonse(buffer);    }

上一篇:前端学HTTP之日志记录
下一篇:URL传值中文乱码

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。