C语言利用http请求网络天气接口json解析开发板显示

用到字库和json库

C语言利用http请求网络天气接口json解析开发板显示_第1张图片

编译命令:

arm-linux-gcc main.c cJSON.c -L./ -lfont -lm -o main 

#include 
#include 
#include 
#include  /* superset of previous */
#include           /* See NOTES */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
/* According to earlier standards */
#include 
#include 
#include 
#include 
#include 
#include  /* superset of previous */
#include           /* See NOTES */
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

//添加json 库文件 
#include "cJSON.h"
#include "font.h"

struct LcdDevice* lcd ;
    
//打开字体	
font *f;
bitmap *bm ;



//初始化Lcd
struct LcdDevice *init_lcd(const char *device)
{
	//申请空间
	struct LcdDevice* lcd = malloc(sizeof(struct LcdDevice));
	if(lcd == NULL)
	{
		return NULL;
	} 

	//1打开设备
	lcd->fd = open(device, O_RDWR);
	if(lcd->fd < 0)
	{
		perror("open lcd fail");
		free(lcd);
		return NULL;
	}
	
	//映射
	lcd->mp = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd->fd,0);

	return lcd;
}

void display(char *buf, int x ,int y)
{

	 bitmap *bm = createBitmapWithInit(800,45,4,getColor(0,200,214,223)); 
    fontSetSize(f,36);
	//将字体写到点阵图上
	fontPrint(f,bm,0,0,buf,getColor(0,255,0,0),0);  

	//把字体框输出到LCD屏幕上
	show_font_to_lcd(lcd->mp,0,0,bm);

	//把字体框输出到LCD屏幕上
	show_font_to_lcd(lcd->mp,x,y,bm);

}

double get_double(cJSON *json, char *key)
{
	cJSON *value = cJSON_GetObjectItem(json,key);
	return cJSON_GetNumberValue(value);
}


char *get_string(cJSON *json, char *key)
{
	cJSON *value = cJSON_GetObjectItem(json,key);
	return cJSON_GetStringValue(value);
}

char * find_weather()
{
    //创建TCP 通信socket 
    int   tcp_socket = socket(AF_INET, SOCK_STREAM, 0);


    //2.连接服务器  (重点)
            struct sockaddr_in addr; 
            addr.sin_family = AF_INET; 
            addr.sin_port   = htons(80) ; //所有HTTP服务器的端口都是 80 
            addr.sin_addr.s_addr =  inet_addr("124.243.226.2");  //青云客服务器 
    int ret=connect(tcp_socket,(struct sockaddr *)&addr,sizeof(addr));
        if(ret < 0)
        {   
            perror("连接服务器失败\n");
        }else
        {
            printf("连接服务器成功\n");
        }


    //3.往服务器中发送请求协议 
//char *http = "GET /api/weather/city/101280101 HTTP/1.1\r\nHost:t.weather.sojson.com\r\n\r\n";
char *http = "GET /csp/api/v2.1/weather?openId=aiuicus&clientType=android&sign=android&city=揭阳&needMoreData=true&pageNo=1&pageSize=7 HTTP/1.1\r\nHost:autodev.openspeech.cn\r\n\r\n";



  write(tcp_socket,http,strlen(http));
    
    //4.读取服务器的应答数据 
    char  data[4096*2]={0};
    read(tcp_socket,data,4096*2);
   // printf("服务器应答:%s\n",data);
    //5.解析数据报  
    // char *p = data; 
    // while (*p!=' ')
    // {
    //     p++;  
    // }

    //状态码
    // char s_code[5]={0}; 
    // strncpy(s_code,p,4);
    // printf("%s\n",s_code);

    // //转换为整形数据 
    // int code = atoi(s_code);
 
    // if(code == 200)
    // {
        printf("请求成功\n");

        //获取响应报体       
       char * p = strstr(data,"{"); 
        //p = strstr(data,"content"); 

         char *p1 = strrchr(p,'}');
         *(p1+1)='\0';
        printf("%s\n",p); 
        return p;

    // }else
    // {
    //     printf("请求失败\n");
    // }


}



int  main()
{
    //初始化Lcd
	lcd = init_lcd("/dev/fb0");
			
	//打开字体	
	f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
	  

 char *string = find_weather();
printf("string:%s\n",string);
    //1.打开获取json 数据 
  //   int fd = open("json.txt",O_RDWR);

  //   char  string[4096]={0}; 
  //   read(fd,string,4096);
  //   printf("%s\n",string);

   //解析JSON 数据包 
   cJSON *json = cJSON_Parse(string);
   if(json == NULL)
   {
       const char *err =  cJSON_GetErrorPtr();  
       fprintf(stderr,"cJSON_Parse fail:%s\n",err);
   }else 
   {
     printf("解析成功\n");
   }

   cJSON *data = cJSON_GetObjectItem( json,"data");
  
		printf("sourceName:%s\n",get_string(data, "sourceName")) ;


    char bufcity[1024] = {0};
    sprintf(bufcity,"sourceName:%s",get_string(data, "sourceName"));
    display(bufcity,0,0);

   cJSON *list = cJSON_GetObjectItem(data,"list");

   int n = cJSON_GetArraySize(list);

   for(int i = 0 ; i < n; i++)
   {
        char bufdate[1024] = {0};
       cJSON * json_str = cJSON_GetArrayItem(list, i);
        //cJSON * json_str= cJSON_Parse(str);
        printf("city:%s\n",get_string(json_str, "city"));
        printf("lastUpdateTime:%s\n",get_string(json_str, "lastUpdateTime"));
        printf("weather:%s\n",get_string(json_str, "weather"));
        printf("temp:%.2f\n",get_double(json_str, "temp"));
        printf("==========\n");
        sprintf(bufdate,"city:%s Time:%s weather:%s temp:%.2f",
        get_string(json_str, "city"),
        get_string(json_str, "lastUpdateTime"),
        get_string(json_str, "weather"),
        get_double(json_str, "temp"));

        display(bufdate,0,50*(i+1));

   }
 
}

你可能感兴趣的:(网络编程,c++,开发语言)