11#include <jni.h>
2- #include <stdio .h>
3- #include <stdlib.h>
2+ #include <sqlite3ext .h>
3+ SQLITE_EXTENSION_INIT1
44
5- static JavaVM * gJvm = NULL ; // Global JVM pointer set by JNI_OnLoad
5+ JavaVM * gJvm = NULL ; // Stored from JNI_OnLoad
66
7- // Called when library is loaded by Android Runtime
87JNIEXPORT jint JNICALL JNI_OnLoad (JavaVM * vm , void * reserved ) {
98 gJvm = vm ;
109 return JNI_VERSION_1_6 ;
1110}
1211
13- // Helper to get JNIEnv for current thread, attach if necessary
14- static JNIEnv * getJNIEnv () {
15- JNIEnv * env = NULL ;
16- if ((* gJvm )-> GetEnv (gJvm , (void * * )& env , JNI_VERSION_1_6 ) != JNI_OK ) {
17- if ((* gJvm )-> AttachCurrentThread (gJvm , (void * * )& env , NULL ) != 0 ) {
12+ JNIEnv * getEnv () {
13+ JNIEnv * env ;
14+ if ((* gJvm )-> GetEnv (gJvm , (void * * )& env , JNI_VERSION_1_6 ) != JNI_OK ) {
15+ if ((* gJvm )-> AttachCurrentThread (gJvm , & env , NULL ) != 0 ) {
1816 return NULL ;
1917 }
2018 }
2119 return env ;
2220}
2321
24- void callHttpsFromC () {
25- JNIEnv * env = getJNIEnv ();
26- if (env == NULL ) {
27- printf ("Failed to get JNIEnv\n" );
28- return ;
29- }
30-
22+ const char * call_https_java (JNIEnv * env ) {
3123 jclass cls = (* env )-> FindClass (env , "HttpsCaller" );
32- if (cls == NULL ) {
33- printf ("Failed to find HttpsCaller class\n" );
34- return ;
35- }
24+ if (!cls ) return "Class not found" ;
3625
3726 jmethodID mid = (* env )-> GetStaticMethodID (env , cls , "callHttps" , "()Ljava/lang/String;" );
38- if (mid == NULL ) {
39- printf ("Failed to find callHttps method\n" );
40- return ;
41- }
27+ if (!mid ) return "Method not found" ;
4228
4329 jstring result = (jstring )(* env )-> CallStaticObjectMethod (env , cls , mid );
44- if (result == NULL ) {
45- printf ("callHttps returned null\n" );
46- return ;
47- }
48-
49- const char * str = (* env )-> GetStringUTFChars (env , result , 0 );
50- printf ("HTTPS Response: %s\n" , str );
51- (* env )-> ReleaseStringUTFChars (env , result , str );
52- }
53-
54- JavaVM * jvm ;
55- JNIEnv * env ;
56-
57- void createJVM () {
58- JavaVMInitArgs vm_args ;
59- JavaVMOption options [1 ];
60- options [0 ].optionString = "-Djava.class.path=." ; // Adjust if needed
61-
62- vm_args .version = JNI_VERSION_1_6 ;
63- vm_args .nOptions = 1 ;
64- vm_args .options = options ;
65- vm_args .ignoreUnrecognized = JNI_FALSE ;
30+ if (!result ) return "Java method returned null" ;
6631
67- jint res = JNI_CreateJavaVM (& jvm , (void * * )& env , & vm_args );
68- if (res != JNI_OK ) {
69- fprintf (stderr , "Failed to create JVM\n" );
70- exit (1 );
71- }
72- }
73-
74- void destroyJVM () {
75- (* jvm )-> DestroyJavaVM (jvm );
76- }
32+ const char * str = (* env )-> GetStringUTFChars (env , result , 0 );
7733
78- int main () {
79- createJVM ();
34+ char * copied = strdup (str );
8035
81- jclass cls = (* env )-> FindClass (env , "HttpsCaller" );
82- if (cls == NULL ) {
83- printf ("Class not found\n" );
84- destroyJVM ();
85- return 1 ;
86- }
87-
88- jmethodID mid = (* env )-> GetStaticMethodID (env , cls , "callHttps" , "()Ljava/lang/String;" );
89- if (mid == NULL ) {
90- printf ("Method not found\n" );
91- destroyJVM ();
92- return 1 ;
93- }
94-
95- jstring result = (jstring )(* env )-> CallStaticObjectMethod (env , cls , mid );
96- const char * str = (* env )-> GetStringUTFChars (env , result , 0 );
97- printf ("Result: %s\n" , str );
9836 (* env )-> ReleaseStringUTFChars (env , result , str );
9937
100- destroyJVM ();
101- return 0 ;
38+ return copied ;
10239}
0 commit comments