【问题标题】:Couln't resolve host name error using curl in Tizen wearable native application在 Tizen 可穿戴本机应用程序中使用 curl 无法解决主机名错误
【发布时间】:2016-10-26 11:31:52
【问题描述】:

您好,我是 Tizen SDK 和开发手表应用程序的新手。使用 curl 发布/获取时遇到错误。下面是我正在使用的代码,有人可以帮忙吗?

CURL *curl;
CURLcode res;

dlog_print(DLOG_DEBUG, TAG, "curl start");

/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);

/* get a curl handle */
curl = curl_easy_init();

if(curl) {
    dlog_print(DLOG_DEBUG, TAG, "curl init");
  /* Set CURL parameters */
    /* First set the URL that is about to receive our POST. This URL can
    just as well be a https:// URL if that is what should receive the
    data. */
    curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
    /* Now specify the POST data */
    //curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "w=4&project=curl");

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if(res != CURLE_OK)
        dlog_print(DLOG_DEBUG, TAG, "curl_easy_perform() Failed: %s\n",curl_easy_strerror(res));
    else{
        dlog_print(DLOG_DEBUG, TAG, "curl_easy_perform() Success");

    }

    /* always cleanup */
    curl_easy_cleanup(curl);
    }
else{
    dlog_print(DLOG_DEBUG, TAG, "curl failed");
}
      curl_global_cleanup();

编辑: 错误是“无法解析主机名”

【问题讨论】:

    标签: curl permissions tizen tizen-wearable-sdk tizen-native-app


    【解决方案1】:

    您可以使用 API 权限尝试这样的代码:

        <privilege>http://tizen.org/privilege/network.get</privilege>
        <privilege>http://tizen.org/privilege/internet</privilege>
    

    此代码只是请求数据并接收该数据。这里

    dlog_print(DLOG_INFO, "data", "Response Data");
    dlog_print(DLOG_INFO, "data", chunk.memory);
    

    用于查看接收到的数据。

    调用函数:

    Eina_Bool sendRequest(void *data EINA_UNUSED) {
    CURL *curl;
    CURLcode curl_err;
    chunk.memory = malloc(1);
    chunk.size = 0;
    curl = curl_easy_init();
    connection_h connection;
    int conn_err;
    conn_err = connection_create(&connection);
    if (conn_err != CONNECTION_ERROR_NONE) {
        /* Error handling */
        return false;
    }
    curl_easy_setopt(curl, CURLOPT_URL,
            "http://apidev.accuweather.com/currentconditions/v1/28143.json?language=en&apikey=hoArfRosT1215");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
    /* we pass our 'chunk' struct to the callback function */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void * )&chunk);
    curl_err = curl_easy_perform(curl);
    if (curl_err != CURLE_OK) {
        /* Error handling */
        return false;
    }
    dlog_print(DLOG_INFO, "data", "Response Data");
    dlog_print(DLOG_INFO, "data", chunk.memory);
    curl_easy_cleanup(curl);
    free(chunk.memory);
    connection_destroy(connection);
    return EINA_TRUE;}
    

    日志输出:

    希望它对你有用。

    【讨论】:

    • 只想问一下*data这个参数是干什么用的?
    • 看来我没有特权。我认为添加功能就足够了。谢谢我现在开始解析。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2017-04-05
    • 2019-12-27
    • 2017-01-03
    • 1970-01-01
    相关资源
    最近更新 更多