Skip to content

Commit

Permalink
Add http get/post request support for Android through JNI.
Browse files Browse the repository at this point in the history
  • Loading branch information
gabordemooij committed Apr 6, 2024
1 parent 7132445 commit f8dbb64
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
47 changes: 47 additions & 0 deletions misc/export/android/MediaHelperAndroid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

/* Copy this to <AndroidProject>/app/src/main/java/org/libsdl/app/MediaHelperAndroid.java */

package org.libsdl.app;

import android.content.Context;

import java.lang.Class;
import java.lang.reflect.Method;

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;

public class MediaHelperAndroid {

public static String httpRequest(String urlstr, String data) throws Exception {
String content = "", line;
try {
URL url = new URL(urlstr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
if (data == null) {
connection.setRequestMethod("GET");
} else {
connection.setRequestMethod("POST");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
writer.write(data);
writer.flush();
writer.close();
}
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = rd.readLine()) != null) {
content += line + "\n";
}
} catch (Exception ex) {
return ex.getMessage();
}
return content;
}
}
19 changes: 19 additions & 0 deletions misc/export/android/citrine.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ extern JNIEXPORT jstring JNICALL Java_com_citrine_citrineandroid_MainActivity_st
}
*/

ctr_object* ctr_network_basic_text_send(ctr_object* myself, ctr_argument* argumentList) {
ctr_object* rs;
char* data = ctr_heap_allocate_cstring(ctr_internal_cast2string(argumentList->object));
char* url = ctr_heap_allocate_cstring(ctr_internal_cast2string(argumentList->next->object));
JNIEnv* env = (JNIEnv*) SDL_AndroidGetJNIEnv();
jclass mha = (*env)->FindClass(env, "org/libsdl/app/MediaHelperAndroid");
jmethodID mid = (*env)->GetStaticMethodID(env, mha, "httpRequest", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
jstring jurl=(*env)->NewStringUTF(env, url);
jstring jdata=(*env)->NewStringUTF(env, data);
jstring result = (jstring) (*env)->CallStaticObjectMethod(env,mha, mid, jurl, jdata);
const char* nativeString = (const char*) (*env)->GetStringUTFChars(env, result, 0);
rs = ctr_build_string_from_cstring( (char*) nativeString);
(*env)->ReleaseStringUTFChars(env, result, nativeString);
(*env)->DeleteLocalRef(env, jurl);
(*env)->DeleteLocalRef(env, jdata);
ctr_heap_free(url);
ctr_heap_free(data);
return rs;
}

static char* embedded_program = "\
Media _datastart.\
Expand Down

0 comments on commit f8dbb64

Please sign in to comment.