当前位置:   article > 正文

鸿蒙OpenHarmony hi3516开发板,标准系统调用外部Rest接口_openharmony libcurl

openharmony libcurl

已实现了标准系统拍照和云服务文字识别的研究,现需要验证在OpenHarmony标准设备上,开发一个应用程序,通过调用OpenHarmony已集成的libcurl,封装2个方法,实现对外网http reset接口(get/post)调用。作为后期调用AI云服务的基础。

本次已经使用OpenHarmony 3.1 Beta的代码,仍然使用润和HiSpark Taurus AI Camera(Hi3516d)开发板套件

步骤1 下载OpenHarmony源代码

  1. repo init -u git@gitee.com:openharmony/manifest.git -b refs/tags/OpenHarmony-v3.1-Beta --no-repo-verify
  2. repo sync -c
  3. repo forall -c 'git lfs pull'

步骤2 增加代码调用http服务

2.1 在applications\standard\httptest新增httptest.cpp,封装httpGet,httpPost方法调用Rest接口

  1. #include<stdio.h>
  2. #include<curl/curl.h>
  3. #include<string>
  4. using namespace std;
  5. static size_t receive_data(void *contents, size_t size, size_t nmemb, void *stream);
  6. // http get
  7. static CURLcode httpGet(const std::string & strUrl, std::string & strResponse,int nTimeout);
  8. // htpp post
  9. static CURLcode httpPost(const std::string & strUrl, std::string szJson,std::string & strResponse,int nTimeout);
  10. static size_t receive_data(void *contents, size_t size, size_t nmemb, void *stream){
  11. string *str = (string*)stream;
  12. (*str).append((char*)contents, size*nmemb);
  13. return size * nmemb;
  14. }
  15. static CURLcode httpGet(const std::string & strUrl, std::string & strResponse,int nTimeout){
  16. CURLcode res;
  17. CURL* pCURL = curl_easy_init();
  18. if (pCURL == NULL) {
  19. return CURLE_FAILED_INIT;
  20. }
  21. curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
  22. //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  23. curl_easy_setopt(pCURL, CURLOPT_NOSIGNAL, 1L);
  24. curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
  25. curl_easy_setopt(pCURL, CURLOPT_NOPROGRESS, 1L);
  26. curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, receive_data);
  27. curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
  28. res = curl_easy_perform(pCURL);
  29. curl_easy_cleanup(pCURL);
  30. return res;
  31. }
  32. static CURLcode httpPost(const std::string & strUrl, std::string szJson,std::string & strResponse,int nTimeout){
  33. CURLcode res;
  34. char szJsonData[1024];
  35. memset(szJsonData, 0, sizeof(szJsonData));
  36. strcpy(szJsonData, szJson.c_str());
  37. CURL* pCURL = curl_easy_init();
  38. struct curl_slist* headers = NULL;
  39. if (pCURL == NULL) {
  40. return CURLE_FAILED_INIT;
  41. }
  42. CURLcode ret;
  43. ret = curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
  44. // std::cout << ret << std::endl;
  45. ret = curl_easy_setopt(pCURL, CURLOPT_POST, 1L);
  46. headers = curl_slist_append(headers,"content-type:application/json");
  47. ret = curl_easy_setopt(pCURL, CURLOPT_HTTPHEADER, headers);
  48. ret = curl_easy_setopt(pCURL, CURLOPT_POSTFIELDS, szJsonData);
  49. //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  50. ret = curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
  51. ret = curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, receive_data);
  52. ret = curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
  53. res = curl_easy_perform(pCURL);
  54. curl_easy_cleanup(pCURL);
  55. return res;
  56. }
  57. int main()
  58. {
  59. printf("hello test http\n");
  60. //string strURL = "http://14.215.177.39";
  61. //string strURL = "http://www.baidu.com";
  62. string strResponse;
  63. CURLcode nRes = huaweiCloud.httpGet(strURL, strResponse,300);
  64. //size_t nSrcLength = strResponse.length();
  65. if (nRes == CURLE_OK){
  66. printf("OK OK OK\n");
  67. }else{
  68. printf("result = %d\n",nRes);
  69. }
  70. printf("%s\n",strResponse.c_str());
  71. printf("end test http\n");
  72. return 0;
  73. }

2.2 在applications\standard\httptest新增Build.gn,编译httptest,依赖libcurl

  1. import("//build/ohos.gni")
  2. import("//drivers/adapter/uhdf2/uhdf.gni")
  3. ohos_executable("httptest") {
  4. sources = [
  5. "httptest.cpp",
  6. "huaweicloud.cpp"
  7. ]
  8. subsystem_name = "applications"
  9. part_name = "prebuilt_hap"
  10. include_dirs = [
  11. "//third_party/curl/include"
  12. ]
  13. deps = [
  14. "//third_party/curl:curl"
  15. ]
  16. }

步骤3 修改配置,增加编译组件

修改applications\standard\hap\ohos.build

module_list里增加 "//applications/standard/httptest:httptest"

步骤4 修改配置,增加编译组件

./build.sh --product-name Hi3516DV300 --ccache

步骤5 DNS修改

目前发现如果请求外部网络是带域名时,会报“无法解析代理”的错误,因此当前只能手动配置DNS服务,或者在配置修改后再编译。这里只写出镜像编译后的修改方式。

  1. mount -oremount,rw / #使etc目录下可写
  2. echo "nameserver 202.96.128.86">/etc/resolv.conf #写入DNS服务地址

步骤6  执行验证

使用串口连接,执行bin目录下的httptest

  1. cd bin
  2. ./httptest

源代码

MyOpenHarmonySample: 我的OpenHarmonySample代码 - Gitee.com

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/294041
推荐阅读
相关标签
  

闽ICP备14008679号