11#include <stdio.h>
2+ #include <jni.h>
23#include "android_https_call.h"
34
45void make_android_https_call (void ) {
5- printf ("Basic test running...\n" );
6+ JavaVM * vms [1 ];
7+ jsize num_vms ;
8+ JNIEnv * env = NULL ;
9+
10+ // Get the Java VM (assumes one has been created)
11+ if (JNI_GetCreatedJavaVMs (vms , 1 , & num_vms ) != JNI_OK || num_vms == 0 ) {
12+ printf ("No Java VM available\n" );
13+ return ;
14+ }
15+
16+ JavaVM * jvm = vms [0 ];
17+
18+ // Attach the current thread to the JVM
19+ if ((* jvm )-> AttachCurrentThread (jvm , (void * * )& env , NULL ) != JNI_OK ) {
20+ printf ("Failed to attach current thread to JVM\n" );
21+ return ;
22+ }
23+
24+ // Locate the HttpsCaller class
25+ jclass cls = (* env )-> FindClass (env , "HttpsCaller" );
26+ if (cls == NULL ) {
27+ printf ("Could not find HttpsCaller class\n" );
28+ (* jvm )-> DetachCurrentThread (jvm );
29+ return ;
30+ }
31+
32+ // Find the static method ID for `callHttps`
33+ jmethodID mid = (* env )-> GetStaticMethodID (env , cls , "callHttps" , "()Ljava/lang/String;" );
34+ if (mid == NULL ) {
35+ printf ("Could not find method callHttps\n" );
36+ (* jvm )-> DetachCurrentThread (jvm );
37+ return ;
38+ }
39+
40+ // Call the static method
41+ jstring result = (jstring )(* env )-> CallStaticObjectMethod (env , cls , mid );
42+
43+ // Convert Java string to C string
44+ const char * str = (* env )-> GetStringUTFChars (env , result , NULL );
45+ printf ("HTTPS result: %s\n" , str );
46+ (* env )-> ReleaseStringUTFChars (env , result , str );
47+
48+ // Detach thread from JVM
49+ (* jvm )-> DetachCurrentThread (jvm );
650}
0 commit comments