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/http.h>
#include <event2/http_struct.h>
#include <event2/http_compat.h>
#include <event2/util.h>
#include <signal.h>
#include <cJSON.h>
#define MYHTTPD_SIGNATURE “MoCarHttpd v0.1”
//处理模块
void httpd_handler(struct evhttp_request *req, void *arg) {
char output[2048] = “\0”;
char tmp[1024];
//获取客户端请求的URI(使用evhttp_request_uri或直接req->uri)
const char *uri;
uri = evhttp_request_uri(req);
#if 0
sprintf(tmp, “uri=%s\n”, uri);// /data?cmd=new…
strcat(output, tmp);
#endif
sprintf(tmp, “uri=%s\n”, req->uri);
strcat(output, tmp);
//decoded uri
char *decoded_uri;
decoded_uri = evhttp_decode_uri(uri);
sprintf(tmp, “decoded_uri=%s\n”, decoded_uri);// /data?cmd= newFile …
strcat(output, tmp);
//http://127.0.0.1:8080/username=gailun&passwd=123123
//解析URI的参数(即GET方法的参数)
struct evkeyvalq params;//key —value, key2— value2// cmd — newfile fromId == 0
//将URL数据封装成key-value格式,q=value1, s=value2
evhttp_parse_query(decoded_uri, ¶ms);
//得到q所对应的value
sprintf(tmp, “username=%s\n”, evhttp_find_header(¶ms, “username”));
strcat(output, tmp);
//得到s所对应的value
sprintf(tmp, “passwd=%s\n”, evhttp_find_header(¶ms, “passwd”));
strcat(output, tmp);
free(decoded_uri);
//获取POST方法的数据
char *post_data = (char *) EVBUFFER_DATA(req->input_buffer);
sprintf(tmp, “post_data=%s\n”, post_data);
strcat(output, tmp);
/*
具体的:可以根据GET/POST的参数执行相应操作,然后将结果输出
…
*/
//入库
/* 输出到客户端 */
//HTTP header
evhttp_add_header(req->output_headers, “Server”, MYHTTPD_SIGNATURE);
evhttp_add_header(req->output_headers, “Content-Type”, “text/plain; charset=UTF-8”);
evhttp_add_header(req->output_headers, “Connection”, “close”);
//输出的内容
struct evbuffer *buf;
buf = evbuffer_new();
evbuffer_add_printf(buf, “It works!\n%s\n”, output);
//将封装好的evbuffer 发送给客户端
evhttp_send_reply(req, HTTP_OK, “OK”, buf);
evbuffer_free(buf);
}
//处理模块
void login_handler(struct evhttp_request *req, void *arg) {
printf("got connection ");
char request_data[4096] = {0};
//获取POST方法的数据
size_t post_size = EVBUFFER_LENGTH(req->input_buffer);
char *post_data = (char *) EVBUFFER_DATA(req->input_buffer);
memcpy(request_data, post_data, post_size);
printf(“post_data = [%s], len =%ld\n”, post_data, post_size);
char username[256] = {0};
char password[256] = {0};
char isDriver[10] = {0};
/*
具体的:可以根据GET/POST的参数执行相应操作,然后将结果输出
…
*/
/*
给服务端的协议
http://ip:port/login [json_data]
{
username: “gailun”,
password: “123123”,
driver: “yes”
}
-
*/
cJSON *root = cJSON_Parse(request_data);
cJSON* username_obj = cJSON_GetObjectItem(root, “username”);
strcpy(username, username_obj->valuestring);
cJSON* password_obj = cJSON_GetObjectItem(root, “password”);
strcpy(password, password_obj->valuestring);
cJSON* isDriver_obj = cJSON_GetObjectItem(root, “driver”);
strcpy(isDriver, isDriver_obj->valuestring);
printf(“username = %s, password = %s, isDriver = %s\n”, username, password, isDriver);
cJSON_Delete(root);
printf("----\n");
//查询数据库 得到查询结果
//给前段回复一个响应结果
cJSON*response_root = cJSON_CreateObject();
cJSON_AddStringToObject(response_root, “result”, “ok”);
char *response_str = cJSON_Print(response_root);
/* 输出到客户端 */
//HTTP header
evhttp_add_header(req->output_headers, “Server”, MYHTTPD_SIGNATURE);
evhttp_add_header(req->output_headers, “Content-Type”, “text/plain; charset=UTF-8”);
evhttp_add_header(req->output_headers, “Connection”, “close”);
//输出的内容
struct evbuffer *buf;
buf = evbuffer_new();
evbuffer_add_printf(buf, “%s”, response_str);
//将封装好的evbuffer 发送给客户端
evhttp_send_reply(req, HTTP_OK, “OK”, buf);
evbuffer_free(buf);
cJSON_Delete(response_root);
free(response_str);
}
void show_help() {
char *help = “http://localhost:8080\n”
“-l <ip_addr> interface to listen on, default is 0.0.0.0\n”
“-p port number to listen on, default is 1984\n”
“-d run as a deamon\n”
“-t timeout for a http request, default is 120 seconds\n”
“-h print this help and exit\n”
“\n”;
fprintf(stderr,"%s",help);
}
//当向进程发出SIGTERM/SIGHUP/SIGINT/SIGQUIT的时候,终止event的事件侦听循环
void signal_handler(int sig) {
switch (sig) {
case SIGTERM:
case SIGHUP:
case SIGQUIT:
case SIGINT:
event_loopbreak(); //终止侦听event_dispatch()的事件侦听循环,执行之后的代码
break;
}
}
int main(int argc, char *argv[]) {
//自定义信号处理函数
signal(SIGHUP, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
//默认参数
char *httpd_option_listen = “0.0.0.0”;
int httpd_option_port = 7777;
int httpd_option_daemon = 0;
int httpd_option_timeout = 120; //in seconds
//获取参数
int c;
while ((c = getopt(argc, argv, “l:p:dt:h”)) != -1) {
switch © {
case ‘l’ :
httpd_option_listen = optarg;
break;
case ‘p’ :
httpd_option_port = atoi(optarg);
break;
case ‘d’ :
httpd_option_daemon = 1;
break;
case ‘t’ :
httpd_option_timeout = atoi(optarg);
break;
case ‘h’ :
default :
show_help();
exit(EXIT_SUCCESS);
}
}
//判断是否设置了-d,以daemon运行
if (httpd_option_daemon) {
pid_t pid;
pid = fork();
if (pid < 0) {
perror(“fork failed”);
exit(EXIT_FAILURE);
}
if (pid > 0) {
//生成子进程成功,退出父进程
exit(EXIT_SUCCESS);
}
}
/* 使用libevent创建HTTP Server */
//初始化event API
event_init();
总结
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的24套腾讯、字节跳动、阿里、百度2019-2021面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
还有 高级架构技术进阶脑图、Android开发面试专题资料 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
点击:《Android架构视频+BAT面试专题PDF+学习笔记》
即可免费获取~
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。
试相关知识,这里放上相关的我搜集整理的24套腾讯、字节跳动、阿里、百度2019-2021面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
还有 高级架构技术进阶脑图、Android开发面试专题资料 帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。
[外链图片转存中…(img-GDwjeONL-1646217572889)]
[外链图片转存中…(img-cKwky5B4-1646217572891)]
[外链图片转存中…(img-GbjPJIfV-1646217572891)]
点击:《Android架构视频+BAT面试专题PDF+学习笔记》
即可免费获取~
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。