curl_easy_setopt(curl, CURLOPT_URL, “http://101.200.190.150:7777/login”);
//2 开启post请求开关
curl_easy_setopt(curl, CURLOPT_POST, true);
//3 添加post数据
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str);
//4 设定一个处理服务器响应的回调函数,deal_response是回调函数
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, deal_response);
//5 给回调函数传递一个形参
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData);
//6 向服务器发送请求,等待服务器的响应
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
__android_log_print(ANDROID_LOG_ERROR,TAG,“JNI-login:perform ERROR, rescode= [%d]\n”,
res);
return JNI_FALSE;
}
//(3) 等待服务器的响应 此刻的responseData就是从服务器获取的数据
/*
//成功
{
result: “ok”,
}
//失败
{
result: “error”,
reason: “why…”
}
-
*/
//(4) 解析服务器返回的json字符串
root = cJSON_Parse(responseData.data);
cJSON *result = cJSON_GetObjectItem(root, “result”);
if(result && strcmp(result->valuestring, “ok”) == 0) {
//登陆成功
__android_log_print(ANDROID_LOG_ERROR,TAG,“JNI-login:login succ!!!”);
return JNI_TRUE;
}
else {
//登陆失败
cJSON* reason = cJSON_GetObjectItem(root, “reason”);
if (reason) {
//已知错误
__android_log_print(ANDROID_LOG_ERROR,TAG,“JNI-login:login error, reason = %s!!!”, reason->valuestring);
}
else {
//未知的错误
__android_log_print(ANDROID_LOG_ERROR,TAG,“JNI-login:login error, reason = Unknow!!!”);
}
return JNI_FALSE;
}
return JNI_TRUE;
}
服务器代码如下http_server.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> //for getopt, fork
#include <string.h> //for strcat
//for struct evkeyvalq
#include <sys/queue.h>
#include <event.h>
//for http
//#include <evhttp.h>
#include <event2/