有没有大神帮忙看看,附件的代码,在执行之后,会出现获取的内容后半段是乱码的情况
试用xr806芯片,开发板,采用example里面的httpc例程,适当修改如下,主要功能就是通过天气接口,获取天气json数据,然后调用cjson接口解析
/*
* Copyright (C) 2017 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include "kernel/os/os.h"
#include "common/framework/platform_init.h"
#include "net/wlan/wlan.h"
#include "common/framework/net_ctrl.h"
#include "net/HTTPClient/HTTPCUsr_api.h"
#include "mbedtls/mbedtls.h"
#include "cjson/cJSON.h"
#define DEMO_TEST_FUNC_TIME 1
#define DEMO_TEST_FUNC_WEATHER 2
#define DEMO_TEST_FUNC DEMO_TEST_FUNC_WEATHER
#define HTTPC_DEMO_THREAD_STACK_SIZE (16 * 1024) /* ssl need more stack */
static OS_Thread_t httpc_demo_thread;
#if (DEMO_TEST_FUNC == DEMO_TEST_FUNC_WEATHER)
# define HTTPC_DEMO_URL "http://t.weather.sojson.com/api/weather/city/101210101"
#else
# define HTTPC_DEMO_URL "http://quan.suning.com/getSysTime.do"
#endif
static HTTPParameters httpc_demo_param;
char httpc_demo_buf[8192] = {0};
static void httpc_demo_download(char *url, int use_ssl)
{
uint32_t download_len = 0;
INT32 recv_len = 0;
int32_t ret;
uint32_t download_time = 0;
memset(&httpc_demo_param, 0, sizeof(httpc_demo_param));
memcpy(httpc_demo_param.Uri, url, strlen(url));
httpc_demo_param.nTimeout = 30; //timeout 30s for every get
download_time = OS_GetTicks();
memset(httpc_demo_buf, 0, sizeof(httpc_demo_buf));
while (1) {
ret = HTTPC_get(&httpc_demo_param, httpc_demo_buf, sizeof(httpc_demo_buf), &recv_len);
if (ret != HTTP_CLIENT_SUCCESS) {
break;
}
download_len += recv_len;
printf("%.*s", sizeof(httpc_demo_buf), httpc_demo_buf);
printf("\n\nhttp get return value: %d, len=%u\n\n", ret, download_len);
}
if (ret == HTTP_CLIENT_EOS) {
download_time = OS_GetTicks() - download_time;
printf("\n\n\ndownload complete, download_len %d, time %ds\n\n\n",
download_len, download_time/1000);
cJSON* cjson = cJSON_Parse(httpc_demo_buf);//将JSON字符串转换成JSON结构体
if(cjson == NULL) //判断转换是否成功
{
printf("cjson error...\n\n");
}
else
{
printf("%s\n\n",cJSON_Print(cjson));//打包成功调用cJSON_Print打印输出
#if (DEMO_TEST_FUNC == DEMO_TEST_FUNC_WEATHER)
char * city = cJSON_GetObjectItem(cjson,"city")->valuestring;
char * temp = cJSON_GetObjectItem(cjson,"wendu")->valuestring;
char * humi = cJSON_GetObjectItem(cjson,"shidu")->valuestring;
printf("城市: %s\n",city);
printf("温度:%s\n",temp);
printf("湿度:%s\n",humi);
#else
char * time_1 = cJSON_GetObjectItem(cjson,"sysTime1")->valuestring;
char * time_2 = cJSON_GetObjectItem(cjson,"sysTime2")->valuestring;
printf("time_1: %s\n\n",time_1);
printf("time_2: %s\n\n",time_2);
#endif
}
} else {
printf("\n\ndownload error, ret=%d\n", ret);
}
}
static void httpc_demo_fun(void *arg)
{
while(1)
{
httpc_demo_download(HTTPC_DEMO_URL, 0);
OS_MSleep(5000);
}
OS_ThreadDelete(&httpc_demo_thread);
}
static void net_cb(uint32_t event, uint32_t data, void *arg)
{
uint16_t type = EVENT_SUBTYPE(event);
switch (type) {
case NET_CTRL_MSG_NETWORK_UP:
if (!OS_ThreadIsValid(&httpc_demo_thread)) {
OS_ThreadCreate(&httpc_demo_thread,
"httpc_demo_thread",
httpc_demo_fun,
(void *)NULL,
OS_THREAD_PRIO_APP,
HTTPC_DEMO_THREAD_STACK_SIZE);
}
break;
case NET_CTRL_MSG_NETWORK_DOWN:
break;
default:
break;
}
}
int main(void)
{
observer_base *net_ob;
platform_init();
printf("httpc demo start\n\n");
printf("use these commands to connect ap:\n\n");
printf("1. config ssid and password : net sta config ssid password\n");
printf("2. enable sta to connect ap : net sta enable\n\n");
/* create an observer to monitor the net work state */
net_ob = sys_callback_observer_create(CTRL_MSG_TYPE_NETWORK,
NET_CTRL_MSG_ALL,
net_cb,
NULL);
if (net_ob == NULL)
return -1;
if (sys_ctrl_attach(net_ob) != 0)
return -1;
return 0;
}
配网之后,拉取http数据,出现了后半段乱码的情形
判了一小段时间更是出现了heap问题
但我看这个heap已经开到了200多KB了,应该不至于这么快出现问题
请大神们帮忙把把关,把把脉,感激不尽!
httpc.rar