This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

LAUNCHCC3235MOD: 在httpget例子基础上修改,想周期获取数据,只有前两次收到数据。请问我应该如何改正?

Part Number: LAUNCHCC3235MOD

TI工程师

您好。

我在httpget的例子基础上加了延时5秒及循环。想定期从服务器获取数据。但是HTTPClient_readResponseBody函数只有第一次循环可以获取数据。

请您指导我一下应该如何修改?

感谢您在百忙中可以看到我的求助,期待您的回答。

/*
 *  ======== httpTask ========
 *  Makes a HTTP GET request
 */
void* httpTask(void* pvParameters)
{
    bool moreDataFlag = false;
    char data[HTTP_MIN_RECV];
    int16_t ret = 0;
    int16_t len = 0;
    while(1){
        sleep(5);
        Display_printf(display, 0, 0, "Sending a HTTP GET request to '%s'\n",
                       HOSTNAME);

        HTTPClient_Handle httpClientHandle;
        int16_t statusCode;
        httpClientHandle = HTTPClient_create(&statusCode,0);
        if(statusCode < 0)
        {
            printError("httpTask: creation of http client handle failed",
                       statusCode);
        }

        ret =
            HTTPClient_setHeader(httpClientHandle,
                                 HTTPClient_HFIELD_REQ_USER_AGENT,
                                 USER_AGENT,strlen(USER_AGENT)+1,
                                 HTTPClient_HFIELD_PERSISTENT);
        if(ret < 0)
        {
            printError("httpTask: setting request header failed", ret);
        }

        ret = HTTPClient_connect(httpClientHandle,HOSTNAME,0,0);
        if(ret < 0)
        {
            printError("httpTask: connect failed", ret);
        }
        ret =
            HTTPClient_sendRequest(httpClientHandle,HTTP_METHOD_GET,REQUEST_URI,
                                   NULL,0,
                                   0);
        if(ret < 0)
        {
            printError("httpTask: send failed", ret);
        }

        if(ret != HTTP_SC_OK)
        {
            printError("httpTask: cannot get status", ret);
        }

        Display_printf(display, 0, 0, "HTTP Response Status Code: %d\n", ret);

        len = 0;
        do
        {
            ret = HTTPClient_readResponseBody(httpClientHandle, data, sizeof(data),
                                              &moreDataFlag);
            if(ret < 0)
            {
                printError("httpTask: response body processing failed", ret);
            }
            Display_printf(display, 0, 0, "%.*s \r\n",ret,data);
            len += ret;
        }
        while(moreDataFlag);

        Display_printf(display, 0, 0, "Received %d bytes of payload\n", len);

        ret = HTTPClient_disconnect(httpClientHandle);
        if(ret < 0)
        {
            printError("httpTask: disconnect failed", ret);
        }

        HTTPClient_destroy(httpClientHandle);
    }
    //return(0);
}