cjson 使用时遇到的问题找不到库

5 篇文章 0 订阅

git clone https://github.com/DaveGamble/cJSON.git

sudo cp -R cJSON/ /usr/include/


遇到的问题

好像需要编译 make  make install  然后再拷贝到 下面的路径

mkdir -p /usr/lib /usr/include/cjson

cp -a cJSON.h /usr/include/cjson

cp -a libcjson.so libcjson.so.1 libcjson.so.1.7.0 /usr/lib

cp -a cJSON_Utils.h /usr/include/cjson

cp -a libcjson_utils.so libcjson_utils.so.1 libcjson_utils.so.1.7.0 /usr/lib

 

 

#include <stdio.h>  

#include <stdlib.h>  

#include <string.h>  

#include <curl/curl.h>  

#include <cJSON/cJSON.h>

//#define POSTURL    "http://www.xiami.com/member/login"  

#define POSTURL    "http://192.168.169.1:10099/reg"  

#define POSTFIELDS "username=dsb12&password=1"  

#define FILENAME   "curlposttest.log"  

 

//解决办法 动态数组 控制buf的大小

typedef struct UserData

{

    char* buf;

    int len;// alloc

    int used; // used length realloc:

}UserData;

 

struct curl_slist* headers = NULL;

 

 

UserData d;

 

size_t write_data(void* buffer,size_t size,size_t nmemb,void *stream)  

{  

    memcpy(d.buf+d.len, buffer, size*nmemb);

    //strcpy(d.buf+d.len, buffer, size*nmemb);

    d.len += size*nmemb;

FILE *fptr = (FILE*)stream;  

fwrite(buffer,size,nmemb,fptr);  

return size*nmemb;  

}  

 

int main(int argc,char *argv[])  

{  

    CURL *curl;  

    CURLcode res;  

    FILE* fptr;  

    struct curl_slist *http_header = NULL;  

 

    if ((fptr = fopen(FILENAME,"w")) == NULL)  

    {  

fprintf(stderr,"fopen file error:%s\n",FILENAME);  

return -1;  

    }  

 

    char username[1024];

    fgets(username, sizeof(username), stdin);

    username[strlen(username)-1]=0;

    char password[1024];

    fgets(password, sizeof(password), stdin);

    password[strlen(password)-1] = 0;

 

    /*

        {

            username: username,

            password: password

        }

 

        {}

    */

 

     cJSON* json = cJSON_CreateObject();

     cJSON_AddItemToObject(json, "username", cJSON_CreateString(username));

     cJSON_AddItemToObject(json, "password", cJSON_CreateString(password));

 

     char* json_buf = cJSON_Print(json);

     printf("%s\n", json_buf);

 

 

     d.buf=(char*)malloc(8192);

 

curl = curl_easy_init();  

if (!curl)  

{  

fprintf(stderr,"curl init failed\n");  

return -1;  

}  

 

curl_easy_setopt(curl,CURLOPT_URL,POSTURL); //url地址  

// curl_easy_setopt(curl,CURLOPT_POSTFIELDS,POSTFIELDS); //post参数  

        curl_easy_setopt(curl,CURLOPT_POSTFIELDS,json_buf); //post参数 不需要统>计长度 因为json是字符串

curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_data); //对返回的数据进行操作的函数地址  

curl_easy_setopt(curl,CURLOPT_WRITEDATA,fptr); //这是write_data的第四个参数值  

curl_easy_setopt(curl,CURLOPT_POST,1); //设置问非0表示本次操作为post  

curl_easy_setopt(curl,CURLOPT_VERBOSE,1); //打印调试信息  

headers=curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

 

// curl_easy_setopt(curl,CURLOPT_HEADER,1); //将响应头信息和相应体一起传给write_data  

curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1); //设置为非0,响应头信息location  

//curl_easy_setopt(curl,CURLOPT_COOKIEFILE,"/Users/zhu/CProjects/curlposttest.cookie");  

 

res = curl_easy_perform(curl);  

printf("============");

printf("%d\n",res);

printf("============");

 

if (res != CURLE_OK)  

{  

switch(res)  

{  

case CURLE_UNSUPPORTED_PROTOCOL:  

fprintf(stderr,"不支持的协议,URL的头部指定\n");  

case CURLE_COULDNT_CONNECT:  

fprintf(stderr,"不能连接到remote主机或者代理\n");  

case CURLE_HTTP_RETURNED_ERROR:  

fprintf(stderr,"http返回错误\n");  

case CURLE_READ_ERROR:  

fprintf(stderr,"读本地文件错误\n");  

default:  

fprintf(stderr,"返回值:%d\n",res);  

}  

return -1;  

}  

curl_slist_free_all(headers); /* free the list again */

curl_easy_cleanup(curl);

printf("%s\n",d.buf);

}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值