赞
踩
已实现了标准系统拍照和云服务文字识别的研究,现需要验证在OpenHarmony标准设备上,开发一个应用程序,通过调用OpenHarmony已集成的libcurl,封装2个方法,实现对外网http reset接口(get/post)调用。作为后期调用AI云服务的基础。
本次已经使用OpenHarmony 3.1 Beta的代码,仍然使用润和HiSpark Taurus AI Camera(Hi3516d)开发板套件
- repo init -u git@gitee.com:openharmony/manifest.git -b refs/tags/OpenHarmony-v3.1-Beta --no-repo-verify
- repo sync -c
- repo forall -c 'git lfs pull'
2.1 在applications\standard\httptest新增httptest.cpp,封装httpGet,httpPost方法调用Rest接口
- #include<stdio.h>
- #include<curl/curl.h>
- #include<string>
-
- using namespace std;
-
- static size_t receive_data(void *contents, size_t size, size_t nmemb, void *stream);
- // http get
- static CURLcode httpGet(const std::string & strUrl, std::string & strResponse,int nTimeout);
- // htpp post
- static CURLcode httpPost(const std::string & strUrl, std::string szJson,std::string & strResponse,int nTimeout);
-
- static size_t receive_data(void *contents, size_t size, size_t nmemb, void *stream){
- string *str = (string*)stream;
- (*str).append((char*)contents, size*nmemb);
- return size * nmemb;
- }
-
-
-
- static CURLcode httpGet(const std::string & strUrl, std::string & strResponse,int nTimeout){
- CURLcode res;
- CURL* pCURL = curl_easy_init();
-
- if (pCURL == NULL) {
- return CURLE_FAILED_INIT;
- }
-
- curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
- //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(pCURL, CURLOPT_NOSIGNAL, 1L);
- curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
- curl_easy_setopt(pCURL, CURLOPT_NOPROGRESS, 1L);
- curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, receive_data);
- curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
- res = curl_easy_perform(pCURL);
- curl_easy_cleanup(pCURL);
- return res;
- }
-
- static CURLcode httpPost(const std::string & strUrl, std::string szJson,std::string & strResponse,int nTimeout){
- CURLcode res;
- char szJsonData[1024];
- memset(szJsonData, 0, sizeof(szJsonData));
- strcpy(szJsonData, szJson.c_str());
- CURL* pCURL = curl_easy_init();
- struct curl_slist* headers = NULL;
- if (pCURL == NULL) {
- return CURLE_FAILED_INIT;
- }
-
- CURLcode ret;
- ret = curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
- // std::cout << ret << std::endl;
-
- ret = curl_easy_setopt(pCURL, CURLOPT_POST, 1L);
- headers = curl_slist_append(headers,"content-type:application/json");
-
- ret = curl_easy_setopt(pCURL, CURLOPT_HTTPHEADER, headers);
-
- ret = curl_easy_setopt(pCURL, CURLOPT_POSTFIELDS, szJsonData);
- //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- ret = curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
-
- ret = curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, receive_data);
-
- ret = curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
-
- res = curl_easy_perform(pCURL);
- curl_easy_cleanup(pCURL);
- return res;
- }
-
-
- int main()
- {
- printf("hello test http\n");
-
- //string strURL = "http://14.215.177.39";
- //string strURL = "http://www.baidu.com";
- string strResponse;
- CURLcode nRes = huaweiCloud.httpGet(strURL, strResponse,300);
-
- //size_t nSrcLength = strResponse.length();
-
- if (nRes == CURLE_OK){
- printf("OK OK OK\n");
- }else{
- printf("result = %d\n",nRes);
- }
-
- printf("%s\n",strResponse.c_str());
-
-
- printf("end test http\n");
-
- return 0;
- }
2.2 在applications\standard\httptest新增Build.gn,编译httptest,依赖libcurl
- import("//build/ohos.gni")
- import("//drivers/adapter/uhdf2/uhdf.gni")
-
- ohos_executable("httptest") {
- sources = [
- "httptest.cpp",
- "huaweicloud.cpp"
- ]
- subsystem_name = "applications"
- part_name = "prebuilt_hap"
-
- include_dirs = [
- "//third_party/curl/include"
- ]
-
-
- deps = [
- "//third_party/curl:curl"
- ]
- }
修改applications\standard\hap\ohos.build
module_list里增加 "//applications/standard/httptest:httptest"
./build.sh --product-name Hi3516DV300 --ccache
目前发现如果请求外部网络是带域名时,会报“无法解析代理”的错误,因此当前只能手动配置DNS服务,或者在配置修改后再编译。这里只写出镜像编译后的修改方式。
- mount -oremount,rw / #使etc目录下可写
- echo "nameserver 202.96.128.86">/etc/resolv.conf #写入DNS服务地址
使用串口连接,执行bin目录下的httptest
- cd bin
- ./httptest
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。