forked from sqliteai/sqlite-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathandroid_https_call.c
More file actions
57 lines (48 loc) · 1.64 KB
/
android_https_call.c
File metadata and controls
57 lines (48 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <jni.h>
#include "android_https_call.h"
int make_android_https_call(void) {
JavaVM *vms[1];
jsize num_vms;
JNIEnv *env = NULL;
jint attached_here = 0;
JNI_GetCreatedJavaVMs(vms, 1, &num_vms);
if (num_vms == 0) {
printf("No Java VM available\n");
return 0;
}
JavaVM *jvm = vms[0];
// Try to get the environment
if ((*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
// Not attached, attach it now
if ((*jvm)->AttachCurrentThread(jvm, (void**)&env, NULL) != JNI_OK) {
printf("Failed to attach current thread to JVM\n");
return 0;
}
attached_here = 1;
}
// // Locate the HttpsCaller class
jclass cls = (*env)->FindClass(env, "com/example/testcloudsync/HttpsCaller");
if (cls == NULL) {
printf("Could not find HttpsCaller class\n");
if (attached_here) (*jvm)->DetachCurrentThread(jvm);
return 0;
}
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "callHttps", "()Ljava/lang/String;");
if (mid == NULL) {
printf("Could not find method callHttps\n");
if (attached_here) (*jvm)->DetachCurrentThread(jvm);
return 0;
}
jstring result = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);
if (result != NULL) {
const char *str = (*env)->GetStringUTFChars(env, result, NULL);
printf("HTTPS result: %s\n", str);
(*env)->ReleaseStringUTFChars(env, result, str);
}
// Detach thread ONLY if we attached it
if (attached_here) {
(*jvm)->DetachCurrentThread(jvm);
}
return (int)num_vms;
}