Я делаю проект Android и пытаюсь отправить запрос на сервер с помощью HTTPS. Чтобы сделать почтовый запрос, мне нужно использовать JNI, поэтому мне нужно реализовать это в C
.
Моя идея состояла в том, чтобы использовать библиотеку, которую я мог бы включить в свой проект, как я сделал с библиотекой минизипов здесь .
Я нашел эту библиотеку здесь , которая казалась мне легкой и могла служить моей цели. Я включил в папку c
, как вы также можете видеть под файлами ca_cert.h
, https.c
и https.h
вместе с папкой mbedtls
точно так же, как это найдено в проекте github.
├── app.iml
├── build.gradle
├── CMakeLists.txt
└── src
├── androidTest
│ └── java
│ └── etc
├── main
│ ├── AndroidManifest.xml
│ ├── c
│ │ ├── ca_cert.h
│ │ ├── https.c
│ │ ├── https.h
│ │ ├── mbedtls
│ │ │ ├── etc
│ │ ├── native-lib.c
│ │ ├── pathfinder.c
│ │ ├── pathfinder.h
│ │ ├── post_data.c
│ │ ├── post_data.h
│ │ ├── third
│ ├── java
│ │ └── etc
│ └── res
│ ├── etc
└── test
└── java
└── etc
Как вы можете видеть в древовидной структуре выше, у меня есть CMakeLists.txt
в корневом каталоге, а в src/main/c/
вы увидите файлы и папку, которые я взял из упомянутой библиотеки https.
Содержимое CMakeLists.txt
следующее
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/c/native-lib.c
src/main/c/ca_cert.h
src/main/c/https.h
)
include_directories(native-lib SHARED src/main/c/mbedtls/include)
#add_definitions(-DHAVE_ZLIB)
find_library( PRE_BUILD_ANDROID_LIB android)
find_library( log-lib log)
find_library(z-lib z)
target_link_libraries( native-lib
${PRE_BUILD_ANDROID_LIB}
${log-lib}
${z-lib}
)
Я что-то упускаю, хотя наверняка, когда я пытаюсь запустить простой пример, подобный следующему
JNIEXPORT jint JNICALL
Java_example_example_do_post(
JNIEnv *env,
jobject this ) {
NSV_LOGE("post_data starts\n");
char *url;
char data[1024], response[4096];
int i, ret, size;
HTTP_INFO hi1, hi2;
// Init http session. verify: check the server CA cert.
http_init(&hi1, FALSE);
http_init(&hi2, TRUE);
url = "http://httpbin.org/get?message=https_client";
ret = http_get(&hi1, url, response, sizeof(response));
return 0;
}
Я получаю
../../../../src/main/c/native-lib.c:56: error: undefined reference to 'http_init'
../../../../src/main/c/native-lib.c:57: error: undefined reference to 'http_init'
../../../../src/main/c/native-lib.c:61: error: undefined reference to 'http_get'
clang: error: linker command failed with exit code 1 (use -v to see invocation)