cnlohr / rawdrawandroid

Build android apps without any java, entirely in C and Make
MIT License
3.55k stars 226 forks source link

http requests #32

Closed pokszef closed 3 years ago

pokszef commented 4 years ago

Hi,

I would like to get internet connection with this project. I tried compile curl with ndk, but gave errors. I can get simple http requests with this library: https://github.com/mattiasgustavsson/libs/blob/main/http.h but I want to have https.

Now I try to to use jni to make java calls to urlconnection, my code so far:

        const struct JNINativeInterface * env = 0;
        const struct JNINativeInterface ** envptr = &env;
        const struct JNIInvokeInterface ** jniiptr = app->activity->vm;
        const struct JNIInvokeInterface * jnii = *jniiptr;
        jnii->AttachCurrentThread( jniiptr, &envptr, NULL);
        env = (*envptr);

        // Retrieves NativeActivity.
        jobject lNativeActivity = gapp->activity->clazz;

        // URL url = new URL(urlString);
        jclass ClassContext = env->FindClass( envptr, "java/net/URL" );
        jmethodID urlConstructor = env->GetMethodID( envptr, ClassContext, "<init>", "(Ljava/lang/String;)V");
        const char url[50] = "http://httpbin.org/anything";
        jstring jurl = env->NewStringUTF( envptr, "http://httpbin.org/anything" );
        jstring jget = env->NewStringUTF( envptr, "GET" );
        jobject urlObj = env->NewObject( envptr, ClassContext, urlConstructor, jurl );

        // urlConnection = (HttpURLConnection) url.openConnection();
        jmethodID openConn = env->GetMethodID( envptr, ClassContext, "openConnection", "()Ljava/net/URLConnection;");
        jobject urlConnectionObj = env->CallObjectMethod( envptr, urlObj, openConn );

        // urlConnection.setRequestMethod("GET");
        jclass urlClass = GetObjectClass( envptr, urlConnectionObj );//env->FindClass( envptr, "java/net/HttpURLConnection" );
        jmethodID setRequestMethod = env->GetMethodID( envptr, urlClass, "setRequestMethod", "(Ljava/lang/String;)V");
        env->CallObjectMethod( envptr, urlConnectionObj, setRequestMethod, jget );

But this code is really not complete and gives errors (I have no idea why).

If anyone tried something similar like this, what did you use? Also where could I learn jni commands? If i get success in this I promise to share.

It can be amazing to get network apps like chat can be under 50kb.

Sorry for bad english

cnlohr commented 4 years ago

I have not needed to do this, so perhaps someone else will hop in with suggestions. I recommend trying out the discord server and seeing if anyone can help out there.