Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem with using the library #62

Open
sadeghhosseini opened this issue Nov 3, 2019 · 0 comments
Open

problem with using the library #62

sadeghhosseini opened this issue Nov 3, 2019 · 0 comments

Comments

@sadeghhosseini
Copy link

sadeghhosseini commented Nov 3, 2019

I am using this library in my android app. The cpp/CMakeLists.txt's content is:

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).
        native-lib.cpp)
find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)
add_library(
    curl
    SHARED
    IMPORTED
)
set_target_properties(
    curl
    PROPERTIES IMPORTED_LOCATION
    ${CMAKE_SOURCE_DIR}/curl-android-ios/prebuilt-with-ssl/android/${ANDROID_ABI}/libcurl.a
)
include_directories(
    ${CMAKE_SOURCE_DIR}/curl-android-ios/prebuilt-with-ssl/android/include/
)
find_library(
    zlib
    z
)
message("***********************" ${zlib})
target_link_libraries( # Specifies the target library.
        native-lib
        curl
        z
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

And this is my cpp/native-lib.cpp file:

#include <jni.h>
#include <string>
#include <curl/curl.h>
#include <stdio.h>
#include <iostream>
#include <android/log.h>
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-lib", __VA_ARGS__))
size_t WriteCallback(char *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}


extern "C" JNIEXPORT jstring JNICALL
Java_com_example_libcurltest_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {

    curl_global_init(CURL_GLOBAL_ALL);

    CURL* easyhandle = curl_easy_init();
    std::string readBuffer;

    curl_easy_setopt(easyhandle, CURLOPT_URL, "https://google.com");
    curl_easy_setopt(easyhandle, CURLOPT_VERBOSE, 1L);
//    curl_easy_setopt(easyhandle, CURLOPT_PROXY, "http://my.proxy.net");   // replace with your actual proxy
//    curl_easy_setopt(easyhandle, CURLOPT_PROXYPORT, 8080L);
    curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(easyhandle, CURLOPT_WRITEDATA, &readBuffer);

    curl_easy_perform(easyhandle);


    __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "\n this is log messge \n");
//    std::cout << readBuffer << std::endl;
    std::string result = "here: " + readBuffer;
    std::string hello = "hello";
    return env->NewStringUTF(result.c_str());
}

But nothing is shown in the textview in android.
Would you please help me fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant