C++学习之打车软件 登录及注册与libcurl集成

目录

 

01 7-Login登陆JNI接口的封装

02 8-Login接口集成json接口

03 9-curl环境的搭建

04 1-libcurl的编程(1)

05 2-libcurl的编程(2)-post请求

06 3-libcurl的编程(3)-处理服务器返回的数据

07 4-Android上部署libcurl

08 5-Android上集成libcurl编程

09 6-libevent的http-server

10 7-登陆模块和服务器通信调试

11 1-reg注册jni接口实现-封装Json类

12 2-reg注册jni接口实现-封装Curl类

 


 

01 7-Login登陆JNI接口的封装

1 将对应Android手机平台的libcurl.a和头文件curl/ 拷贝到jni路径下。
2 将jni生成的so文件 联通libcurl.a一起编译
需要修改Android.mk文件
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
# libcurl.a
LOCAL_MODULE := libcurl
LOCAL_SRC_FILES := libcurl.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
#libOBOJni.so
LOCAL_MODULE := OBOJni
LOCAL_SRC_FILES := test.cpp Login.cpp cJSON.cpp
LOCAL_LDLIBS := -llog -lz
LOCAL_STATIC_LIBRARIES := libcurl
#要生成的是一个so动态库
include $(BUILD_SHARED_LIBRARY)
3 因为目前jni中只有一个libcurl.a 需要在build的时候 只编译一个平台的so
在jni路径中 创建Application.mk
添加
APP_ABI = armeabi
4 配置AndroidStudio的平台版本 只编译armeabi平台
在项目/app/build.gradle
A

02 8-Login接口集成json接口

在里面的 android{}-->defaultConfit {} 作用范围内添加
 
// 不声明ndk标签,项目默认会创建一个libapp.so的文件
 
ndk {
 
// 声明创建so库的文件名,会自动添加lib前缀, 添加了前缀,不会自动添加
 
moduleName "OBOjni"
 
//声明启用Android日志, 在c/c++的源文件中使用的#include <android/log.h> 日志
将得到输出
 
//这里我们关联了两个库 一个是liblog 和 libz
 
ldLibs "log","z"
 
// 声明创建指定cpu架构的so库, 不声明的话, 默认(gradle 1.5.0)会生成7中架构,如果
你的libcurl没有提供别的平台,那么就会链接失败,
 
//所以此条配置很重要,这里我们只生成一个平台
 
abiFilters "armeabi"
 
}

 

03 9-curl环境的搭建

5 点击Sync Now
6 ndk-build
7 修改Login.cpp
#include <curl/curl.h>
在代码实现中 测试接口
CURL *curl = NULL;
curl = curl_easy_init();
8 ndk-build 如果能够成功生成so 说明编译没有问题

04 1-libcurl的编程(1)

9 将应用程序部署在android手机上, 点击登录按钮 触发刚才的curl的接口,如果不崩
溃,就表示curl的环境部署成功

05 2-libcurl的编程(2)-post请求

#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/data?cmd=newFile&fromId=0&count=8
 
    //解析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, &params);
 
    //得到q所对应的value
    sprintf(tmp, "username=%s\n", evhttp_find_header(&params, "username"));
    strcat(output, tmp);
    //得到s所对应的value
    sprintf(tmp, "passwd=%s\n", evhttp_find_header(&params, "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的参数执行相应操作,然后将结果输出
       ...
     */
    printf("get msg, send : [%s]\n", output);
 
 
    /* 输出到客户端 */
 
    //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);
 
}

06 3-libcurl的编程(3)-处理服务器返回的数据


void show_help() {
    char *help = "http://localhost:8080\n"
        "-l <ip_addr> interface to listen on, default is 0.0.0.0\n"
        "-p <num>     port number to listen on, default is 1984\n"
        "-d           run as a deamon\n"
        "-t <second>  timeout for a http request, default is 120 seconds\n"
        "-h           print this help and exit\n"
        "\n";
    fprintf(stderr,"%s",help);
}

07 4-Android上部署libcurl


//当向进程发出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;
    }
}

08 5-Android上集成libcurl编程


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 = 8080;
    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 (c) {
            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();

    //创建一个http server
    struct evhttp *httpd;

    httpd = evhttp_start(httpd_option_listen, httpd_option_port);

    evhttp_set_timeout(httpd, httpd_option_timeout);

    //指定generic callback
    // evhttp_set_gencb(httpd, httpd_handler, NULL);
    //也可以为特定的URI指定callback
    evhttp_set_cb(httpd, "/", httpd_handler, NULL);
    evhttp_set_cb(httpd, "/login", login_handler,NULL);

    //循环处理events
    event_dispatch();

    evhttp_free(httpd);

    return 0;
}

09 6-libevent的http-server

 

10 7-登陆模块和服务器通信调试

 

11 1-reg注册jni接口实现-封装Json类

 

12 2-reg注册jni接口实现-封装Curl类

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值