1.项目功能
通过此程序可方便获取NowAPI 数据接口服务商 诺派数据 网站的天气信息,可实现简单页面输入,根据用户输入,输出相应天气结果,并可以将用户查询到的历史信息保存在log文件中。
按功能可分为以下模块:网络套接字初始模块、ui显示模块、命令接收及分析模块、执行命令模块、日志写入模块。
#include"head.h"
#include"cJSON.h"
int cmd = 0;
char cityname[16] ={0};
char bigbuff[4096] = {0};
char date[4096] = {0};
char day[16] ={0};
int init_tcp_cli(const char *ip,unsigned short port)
{
int sockfd = socket(AF_INET,SOCK_STREAM,0);
if(-1 == sockfd)
{
perror("fail socket");
return -1;
}
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_port =htons(port);
ser.sin_addr.s_addr = inet_addr(ip);
int ret = connect(sockfd,(struct sockaddr*)&ser,sizeof(ser));
if(-1 == ret )
{
perror("error connect!");
return -1;
}
return sockfd;
}
int send_http_today_response(int sockfd)
{
char buff1[1024]={0};
sprintf(buff1,"GET /?app=weather.%s&cityNm=%s&appkey=73565&sign=6d208c1c8a7c71712037c7063e1d17fd&format=json HTTP/1.1\r\n"
"Host: api.k780.com\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8\r\n"
"Accept-Language: en-US,en;5q=0.5\r\n"
"Connection: close\r\n\r\n",day,cityname);
char *prep = buff1;
size_t size = send(sockfd,prep,strlen(prep),0);
if(size < 0)
{
return -1;
}
return 0;
}
int recv_http_today_response(int sockfd)
{
char buff[1024] ={0};
while(1)
{
memset(buff,0,sizeof(buff));
size_t size = recv(sockfd,buff,sizeof(buff)-1,0);
if(size < 0)
{
perror("fail recv");
return -1;
}
else if(0 == size)
{
break;
}
strcat(bigbuff,buff);
}
char *p;
p = strchr(bigbuff, '{');
strcpy(date, p);
return 0;
}
int recv_http_today_response1(int sockfd)
{
char buff[1024] ={0};
while(1)
{
memset(buff,0,sizeof(buff));
size_t size = recv(sockfd,buff,sizeof(buff)-1,0);
if(size < 0)
{
perror("fail recv");
return -1;
}
else if(0 == size)
{
break;
}
strcat(bigbuff,buff);
}
char *p;
p = strchr(bigbuff, '{');
strcpy(date, p);
return 0;
}
int show_cmd(void)
{
printf("输入想要查询的城市\n");
scanf("%s",cityname);
printf("查询未来天气输入1\n");
printf("查询当前天气输入2\n");
scanf("%d",&cmd );
if(1 == cmd)
{
printf("当前城市:%s\n",cityname);
}
else if(2 == cmd)
{
printf("当前城市:%s\n",cityname);
}
return 0;
}
int cjson_hander()
{
cJSON *item;
cJSON* root = cJSON_Parse(date);
if(root == NULL)
{
printf("pasrse fail.\n");
return -1;
}
cJSON* object =cJSON_GetObjectItem(root,"result");
item =cJSON_GetObjectItem(object,"days");
printf("%s\n", item->valuestring);
item=cJSON_GetObjectItem(object,"week");
printf("%s\n", item->valuestring);
item=cJSON_GetObjectItem(object,"temperature");
printf("%s\n", item->valuestring);
item=cJSON_GetObjectItem(object,"humidity");
printf("%s\n", item->valuestring);
item=cJSON_GetObjectItem(object,"aqi");
printf("%s\n", item->valuestring);
item=cJSON_GetObjectItem(object,"weather");
printf("%s\n", item->valuestring);
item=cJSON_GetObjectItem(object,"wind");;
printf("%s\n", item->valuestring);
cJSON_Delete(root);
return 0;
}
int cjson_hander1()
{
char data_num[7] = {0};
cJSON* root = cJSON_Parse(date);
cJSON* arrayitem =cJSON_GetObjectItem(root,"result");
int kind = cJSON_GetArraySize(arrayitem);
printf("一共%d组\n",kind);
for(int i = 0;i<kind;i++)
{
printf("第%d组\n",i+1);
cJSON *Object = cJSON_GetArrayItem(arrayitem, i);
cJSON *item = cJSON_GetObjectItem(Object, "days");
printf("%s\n", (char *)item->valuestring);
item = cJSON_GetObjectItem(Object, "week");
printf("%s\n", (char *)item->valuestring);
item = cJSON_GetObjectItem(Object, "citynm");
printf("%s\n", (char *)item->valuestring);
item = cJSON_GetObjectItem(Object, "temperature");
printf("温度范围: ");
printf("%s\n", (char *)item->valuestring);
printf("天气: ");
item = cJSON_GetObjectItem(Object, "weather");
printf("%s\n", (char *)item->valuestring);
printf("\n");
}
cJSON_Delete(root);
return 0;
}
int main()
{
int sockfd =init_tcp_cli("103.205.5.228",80);
show_cmd();
if(2 == cmd)
{
sprintf(day,"today");
send_http_today_response(sockfd);
recv_http_today_response(sockfd);
cjson_hander();
close(sockfd);
}
else if(1 == cmd)
{
sprintf(day,"future");
send_http_today_response(sockfd);
recv_http_today_response(sockfd);
cjson_hander1();
close(sockfd);
}
return 0;
}

441

被折叠的 条评论
为什么被折叠?



