Skip to content

Commit 2a8fe5c

Browse files
committed
test
1 parent 3543e6b commit 2a8fe5c

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

src/HttpsCaller.class

1.76 KB
Binary file not shown.

src/HttpsCaller.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.net.*;
2+
import java.io.*;
3+
import javax.net.ssl.HttpsURLConnection;
4+
5+
public class HttpsCaller {
6+
public static String callHttps() {
7+
try {
8+
URL url = new URL("https://api.github.com/");
9+
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
10+
conn.setRequestMethod("GET");
11+
12+
BufferedReader in = new BufferedReader(
13+
new InputStreamReader(conn.getInputStream()));
14+
String inputLine;
15+
StringBuilder content = new StringBuilder();
16+
17+
while ((inputLine = in.readLine()) != null) {
18+
content.append(inputLine);
19+
}
20+
in.close();
21+
conn.disconnect();
22+
return content.toString();
23+
} catch (Exception e) {
24+
return "Error: " + e.getMessage();
25+
}
26+
}
27+
}

src/call_java

33.3 KB
Binary file not shown.

src/call_java.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <jni.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
static JavaVM *gJvm = NULL; // Global JVM pointer set by JNI_OnLoad
6+
7+
// Called when library is loaded by Android Runtime
8+
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
9+
gJvm = vm;
10+
return JNI_VERSION_1_6;
11+
}
12+
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) {
18+
return NULL;
19+
}
20+
}
21+
return env;
22+
}
23+
24+
void callHttpsFromC() {
25+
JNIEnv *env = getJNIEnv();
26+
if (env == NULL) {
27+
printf("Failed to get JNIEnv\n");
28+
return;
29+
}
30+
31+
jclass cls = (*env)->FindClass(env, "HttpsCaller");
32+
if (cls == NULL) {
33+
printf("Failed to find HttpsCaller class\n");
34+
return;
35+
}
36+
37+
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+
}
42+
43+
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;
66+
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+
}
77+
78+
int main() {
79+
createJVM();
80+
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);
98+
(*env)->ReleaseStringUTFChars(env, result, str);
99+
100+
destroyJVM();
101+
return 0;
102+
}

0 commit comments

Comments
 (0)