Skip to content

Commit de9e6f8

Browse files
committed
ANDROID: Implemented "android.request"
1 parent 4ce4b5f commit de9e6f8

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/MainActivity.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public class MainActivity extends NativeActivity {
103103
private static final String FOLDER_NAME = "SmallBASIC";
104104
private static final int COPY_BUFFER_SIZE = 1024;
105105
private static final String[] SAMPLES = {"welcome.bas"};
106+
private static final int TIMEOUT_MILLIS = 5000;
106107
private String _startupBas = null;
107108
private boolean _untrusted = false;
108109
private final ExecutorService _audioExecutor = Executors.newSingleThreadExecutor();
@@ -382,7 +383,6 @@ public boolean loadModules() {
382383
System.loadLibrary("ioio");
383384
Class.forName("ioio.smallbasic.android.ModuleLoader")
384385
.getDeclaredConstructor(Long.class, Context.class).newInstance(getActivity(), this);
385-
consoleLog("loadModules - success");
386386
result = true;
387387
} catch (Exception | UnsatisfiedLinkError e) {
388388
consoleLog(e.toString());
@@ -509,10 +509,10 @@ public boolean removeLocationUpdates() {
509509
return result;
510510
}
511511

512-
public String request(String endPoint, String method, String data, String apiKey) throws IOException {
512+
public String request(String endPoint, String data, String apiKey) throws IOException {
513513
String result;
514514
try {
515-
HttpURLConnection conn = getHttpURLConnection(endPoint, method, apiKey);
515+
HttpURLConnection conn = getHttpURLConnection(endPoint, (data == null || data.isEmpty()) ? "GET" : "POST", apiKey);
516516
if (data != null && !data.isEmpty()) {
517517
OutputStream os = conn.getOutputStream();
518518
os.write(data.getBytes(StandardCharsets.UTF_8));
@@ -530,10 +530,10 @@ public String request(String endPoint, String method, String data, String apiKey
530530
in.close();
531531
result = response.toString();
532532
} else {
533-
result = "[error:" + responseCode + "]";
533+
result = "error:[" + responseCode + "]";
534534
}
535535
} catch (Exception e) {
536-
result = "[error:" + e + "]";
536+
result = "error:[" + e + "]";
537537
}
538538
return result;
539539
}
@@ -798,21 +798,19 @@ private void execStream(InputStream inputStream) throws IOException {
798798
}
799799

800800
@NonNull
801-
private static HttpURLConnection getHttpURLConnection(String endPoint,
802-
String method,
803-
String apiKey) throws IOException {
801+
private HttpURLConnection getHttpURLConnection(String endPoint, String method, String apiKey) throws IOException {
804802
URL url = new URL(endPoint);
805-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
806-
conn.setConnectTimeout(10000);
807-
conn.setRequestProperty("User-Agent", "SmallBASIC");
808-
conn.setRequestMethod(method == null || method.isEmpty() ? "POST" : method);
809-
conn.setInstanceFollowRedirects(true);
803+
HttpURLConnection result = (HttpURLConnection) url.openConnection();
804+
result.setConnectTimeout(TIMEOUT_MILLIS);
805+
result.setRequestProperty("User-Agent", "SmallBASIC");
806+
result.setRequestMethod(method);
807+
result.setInstanceFollowRedirects(true);
810808
if (apiKey != null && !apiKey.isEmpty()) {
811-
conn.setRequestProperty("Content-Type", "application/json");
812-
conn.setRequestProperty("Authorization", "Bearer " + apiKey);
809+
result.setRequestProperty("Content-Type", "application/json");
810+
result.setRequestProperty("Authorization", "Bearer " + apiKey);
813811
}
814-
conn.setDoOutput(true);
815-
return conn;
812+
result.setDoOutput(true);
813+
return result;
816814
}
817815

818816
private Uri getSharedFile(File file) {

src/platform/android/jni/runtime.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ extern "C" JNIEXPORT void JNICALL Java_net_sourceforge_smallbasic_MainActivity_c
200200
if (jstr != nullptr) {
201201
const char *str = env->GetStringUTFChars(jstr, 0);
202202
runtime->systemLog(str);
203+
runtime->systemLog("\n");
203204
env->ReleaseStringUTFChars(jstr, str);
204205
}
205206
}
@@ -976,30 +977,27 @@ int Runtime::getFontId() {
976977

977978
int Runtime::invokeRequest(int argc, slib_par_t *params, var_t *retval) {
978979
int result = 0;
979-
if ((argc >= 1 && argc <= 4 && v_is_type(params[0].var_p, V_STR)) &&
980+
if ((argc >= 1 && argc <= 3 && v_is_type(params[0].var_p, V_STR)) &&
980981
(argc < 2 || v_is_type(params[1].var_p, V_STR)) &&
981-
(argc < 3 || v_is_type(params[2].var_p, V_STR)) &&
982-
(argc < 4 || v_is_type(params[3].var_p, V_STR))) {
982+
(argc < 3 || v_is_type(params[2].var_p, V_STR))) {
983983
JNIEnv *env;
984984
_app->activity->vm->AttachCurrentThread(&env, nullptr);
985985

986986
auto endPoint = env->NewStringUTF(v_getstr(params[0].var_p));
987-
auto method = env->NewStringUTF(argc < 2 ? "POST" : v_getstr(params[1].var_p));
988987
auto data = env->NewStringUTF(argc < 3 ? "" : v_getstr(params[2].var_p));
989988
auto apiKey = env->NewStringUTF(argc < 4 ? "" : v_getstr(params[3].var_p));
990989

991990
jclass clazz = env->GetObjectClass(_app->activity->clazz);
992-
const char *signature = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;";
991+
const char *signature = "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;";
993992
jmethodID methodId = env->GetMethodID(clazz, "request", signature);
994-
jstring jstr = (jstring)env->CallObjectMethod(_app->activity->clazz, methodId, endPoint, method, data, apiKey);
993+
jstring jstr = (jstring)env->CallObjectMethod(_app->activity->clazz, methodId, endPoint, data, apiKey);
995994
const char *str = env->GetStringUTFChars(jstr, JNI_FALSE);
996995
v_setstr(retval, str);
997-
result = strncmp(str, "[error:", 7) == 0 ? 0 : 1;
996+
result = strncmp(str, "error:[", 7) == 0 ? 0 : 1;
998997
env->ReleaseStringUTFChars(jstr, str);
999998
env->DeleteLocalRef(jstr);
1000999
env->DeleteLocalRef(clazz);
10011000
env->DeleteLocalRef(endPoint);
1002-
env->DeleteLocalRef(method);
10031001
env->DeleteLocalRef(data);
10041002
env->DeleteLocalRef(apiKey);
10051003

0 commit comments

Comments
 (0)