Skip to content

Commit 005c35c

Browse files
committed
use package name in findClass
1 parent 97c5298 commit 005c35c

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/android_https_call.c

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,56 @@
22
#include <jni.h>
33
#include "android_https_call.h"
44

5-
void make_android_https_call(void) {
5+
int make_android_https_call(void) {
66
JavaVM *vms[1];
77
jsize num_vms;
88
JNIEnv *env = NULL;
9+
jint attached_here = 0;
910

10-
// Get the Java VM (assumes one has been created)
11-
if (JNI_GetCreatedJavaVMs(vms, 1, &num_vms) != JNI_OK || num_vms == 0) {
11+
JNI_GetCreatedJavaVMs(vms, 1, &num_vms);
12+
13+
if (num_vms == 0) {
1214
printf("No Java VM available\n");
13-
return;
15+
return 0;
1416
}
1517

1618
JavaVM *jvm = vms[0];
1719

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;
20+
// Try to get the environment
21+
if ((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
22+
// Not attached, attach it now
23+
if ((*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL) != JNI_OK) {
24+
printf("Failed to attach current thread to JVM\n");
25+
return 0;
26+
}
27+
attached_here = 1;
2228
}
23-
24-
// Locate the HttpsCaller class
25-
jclass cls = (*env)->FindClass(env, "HttpsCaller");
29+
// // Locate the HttpsCaller class
30+
jclass cls = (*env)->FindClass(env, "com/example/testcloudsync/HttpsCaller");
2631
if (cls == NULL) {
2732
printf("Could not find HttpsCaller class\n");
28-
(*jvm)->DetachCurrentThread(jvm);
29-
return;
33+
if (attached_here) (*jvm)->DetachCurrentThread(jvm);
34+
return 0;
3035
}
3136

32-
// Find the static method ID for `callHttps`
3337
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "callHttps", "()Ljava/lang/String;");
3438
if (mid == NULL) {
3539
printf("Could not find method callHttps\n");
36-
(*jvm)->DetachCurrentThread(jvm);
37-
return;
40+
if (attached_here) (*jvm)->DetachCurrentThread(jvm);
41+
return 0;
3842
}
3943

40-
// Call the static method
4144
jstring result = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);
45+
if (result != NULL) {
46+
const char *str = (*env)->GetStringUTFChars(env, result, NULL);
47+
printf("HTTPS result: %s\n", str);
48+
(*env)->ReleaseStringUTFChars(env, result, str);
49+
}
4250

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);
51+
// Detach thread ONLY if we attached it
52+
if (attached_here) {
53+
(*jvm)->DetachCurrentThread(jvm);
54+
}
4755

48-
// Detach thread from JVM
49-
(*jvm)->DetachCurrentThread(jvm);
56+
return (int)num_vms;
5057
}

src/android_https_call.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef ANDROID_HTTPS_CALL_H
22
#define ANDROID_HTTPS_CALL_H
33

4-
void make_android_https_call(void);
4+
int make_android_https_call(void);
55

66
#endif

0 commit comments

Comments
 (0)