讲解如何在Neptune开发板中发送GET请求去获取bilibili粉丝数,并通过CJSON解析回传的数据将获取到bilibili粉丝数输出至串口。
1、环境准备
1.1 开发环境、编译环境搭建,参考官方文档,参考链接如下: Neptune开发板的环境搭建及使用
1.2 Neptune开发板 openharmony V1.1.0 LTS 版本获取
git clone https://gitee.com/hihopeorg_group/neptune-harmony-os1.1-iot.git
2、材料准备
1、Neptune HarmonyOS物联网 IOT模组
3、使用库介绍
由于W800上已经移植了lwip(一个小型开源的TCP/IP协议栈),所以在开发板上面进行网络编程相对来说还是比较方便,相关的API:
//device/winnermicro/neptune/sdk_liteos/src/network/lwip2.0.3/include
关于CJSON库使用openHarmony 提供的第三方CJSON库,位置在于:
//third_party/cJSON
如需使用需要将"
//build/lite/config/component/cJSON:cjson_static", 添加到"
//device/winnermicro/neptune/sdk_liteos/BUILD.gn"中
4、编写代码
4.1、首先需要让开发板连接上网络,这里使用润和官方提供的wifi_connecter.h 中API连接网络:
WifiDeviceConfig config = {0}; // 准备AP的配置参数 strcpy(config.ssid, PARAM_HOTSPOT_SSID); strcpy(config.preSharedKey, PARAM_HOTSPOT_PSK); config.securityType = WIFI_SEC_TYPE_PSK; osDelay(10); //连接Wifi int netId = ConnectToHotspot(&config);
4.2、bilibili粉丝数的获取原理:
使用固定的链接加用户ID:
api.bilibili.com/x/relation/stat?vmid=+B站用户uid
例如新华社UID:473837611是,地址为:
api.bilibili.com/x/relation/stat?vmid=473837611
直接在浏览器里面输入就可以得到数据
{"code":0,"message":"0","ttl":1,"data":{"mid":473837611,"following":52,"whisper":0,"black":0,"follower":2971170}}
通过在线CJSON解析后为:
可以看到其follower(粉丝数)值。
4.3、定义GET请求的header
//http组包宏,获取bilibili的http接口参数#define WEB_SERVER "api.bilibili.com" #define WEB_PORT "80"#define WEB_URL "/x/relation/stat?vmid=用户UID"//http请求包static const char *REQUEST = "GET "WEB_URL" HTTP/1.1\r\n" "Host: "WEB_SERVER":"WEB_PORT"\r\n" "Connection: close\r\n" "\r\n";
4.4、创建一个Socket并连接以及写入请求
const struct addrinfo hints = { .ai_family = AF_INET, .ai_socktype = SOCK_STREAM, }; struct addrinfo *res; struct in_addr *addr; int sockfd,response; char recv_buf[1024]; char response_buf[1024]; int index; while(1) { //DNS域名解析 int err = getaddrinfo(WEB_SERVER,WEB_PORT,&hints,&res); if(err != 0 || res == NULL) { printf("DNS lookup failed err=%d res=%p\r\n", err, res); osDelay(50); continue; } //打印获取的IP addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; //printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(*addr)); //新建socket sockfd = socket(res->ai_family, res->ai_socktype, 0); if(sockfd < 0) { printf("... Failed to allocate socket.\r\n"); close(sockfd); freeaddrinfo(res); osDelay(50); continue; } //连接ip if(connect(sockfd, res->ai_addr, res->ai_addrlen) != 0) { //printf("... socket connect failed errno=%d\r\n", errno); perror("socket connect failed errno\r\n"); close(sockfd); freeaddrinfo(res); osDelay(50); continue; } freeaddrinfo(res); //发送http包 if (write(sockfd, REQUEST, strlen(REQUEST)) < 0) { printf("... socket send failed\r\n"); close(sockfd); osDelay(50); continue; }
4.5、接受获取到的数据
do { memset(recv_buf,0,sizeof(recv_buf)); response = read(sockfd, recv_buf, sizeof(recv_buf)-1); strcat(response_buf,recv_buf); } while(response > 0);
4.6、解析json数据,由于上述以讲述cjson数据的构成所以直接书写json数据函数,并将解析粉丝数打印:
void cjson_struct_info(char *text){ cJSON *root,*data; cJSON *follower; //截取有效json char *index=strchr(text,'{'); strcpy(text,index); root = cJSON_Parse(text); if(root!=NULL) { data = cJSON_GetObjectItem(root, "data"); follower = cJSON_GetObjectItem(data,"follower"); printf("follower:%d\r\n",follower->valueint); } cJSON_Delete(root); }
4.7、编译、烧录,并测试最后的完成效果。
——————
原创:老王丨鸿蒙hms开发者高级认证持证人!学习更多鸿蒙OS相关开发技术可以关注我的公众号:鸿蒙开发者老王