|
38 | 38 | import android.view.inputmethod.InputMethodManager; |
39 | 39 | import android.widget.Toast; |
40 | 40 |
|
41 | | -import androidx.annotation.NonNull; |
42 | 41 | import androidx.core.app.ActivityCompat; |
43 | 42 | import androidx.core.content.ContextCompat; |
44 | 43 | import androidx.core.content.FileProvider; |
|
59 | 58 | import java.io.InputStreamReader; |
60 | 59 | import java.io.OutputStream; |
61 | 60 | import java.io.UnsupportedEncodingException; |
62 | | -import java.net.HttpURLConnection; |
63 | 61 | import java.net.Inet4Address; |
64 | 62 | import java.net.InetAddress; |
65 | 63 | import java.net.NetworkInterface; |
66 | 64 | import java.net.SocketException; |
67 | | -import java.net.URL; |
68 | 65 | import java.net.URLDecoder; |
69 | | -import java.nio.charset.StandardCharsets; |
70 | 66 | import java.util.ArrayList; |
71 | 67 | import java.util.Collection; |
72 | 68 | import java.util.Date; |
@@ -103,7 +99,6 @@ public class MainActivity extends NativeActivity { |
103 | 99 | private static final String FOLDER_NAME = "SmallBASIC"; |
104 | 100 | private static final int COPY_BUFFER_SIZE = 1024; |
105 | 101 | private static final String[] SAMPLES = {"welcome.bas"}; |
106 | | - private static final int TIMEOUT_MILLIS = 5000; |
107 | 102 | private String _startupBas = null; |
108 | 103 | private boolean _untrusted = false; |
109 | 104 | private final ExecutorService _audioExecutor = Executors.newSingleThreadExecutor(); |
@@ -510,33 +505,8 @@ public boolean removeLocationUpdates() { |
510 | 505 | } |
511 | 506 |
|
512 | 507 | public String request(String endPoint, String data, String apiKey) throws IOException { |
513 | | - String result; |
514 | | - try { |
515 | | - HttpURLConnection conn = getHttpURLConnection(endPoint, (data == null || data.isEmpty()) ? "GET" : "POST", apiKey); |
516 | | - if (data != null && !data.isEmpty()) { |
517 | | - conn.setRequestProperty("Content-Length", "" + data.getBytes().length); |
518 | | - OutputStream os = conn.getOutputStream(); |
519 | | - os.write(data.getBytes(StandardCharsets.UTF_8)); |
520 | | - os.flush(); |
521 | | - os.close(); |
522 | | - } |
523 | | - int responseCode = conn.getResponseCode(); |
524 | | - if (responseCode == HttpURLConnection.HTTP_OK) { |
525 | | - BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
526 | | - String inputLine; |
527 | | - StringBuilder response = new StringBuilder(); |
528 | | - while ((inputLine = in.readLine()) != null) { |
529 | | - response.append(inputLine); |
530 | | - } |
531 | | - in.close(); |
532 | | - result = response.toString(); |
533 | | - } else { |
534 | | - result = "error: [" + responseCode + "]"; |
535 | | - } |
536 | | - } catch (Exception e) { |
537 | | - result = "error: [" + e + "]"; |
538 | | - } |
539 | | - return result; |
| 508 | + HttpConnection connection = new HttpConnection(endPoint, data, apiKey); |
| 509 | + return connection.invoke(); |
540 | 510 | } |
541 | 511 |
|
542 | 512 | public boolean requestLocationUpdates() { |
@@ -798,23 +768,6 @@ private void execStream(InputStream inputStream) throws IOException { |
798 | 768 | runFile(outputFile.getAbsolutePath()); |
799 | 769 | } |
800 | 770 |
|
801 | | - @NonNull |
802 | | - private HttpURLConnection getHttpURLConnection(String endPoint, String method, String apiKey) throws IOException { |
803 | | - URL url = new URL(endPoint); |
804 | | - HttpURLConnection result = (HttpURLConnection) url.openConnection(); |
805 | | - result.setConnectTimeout(TIMEOUT_MILLIS); |
806 | | - result.setRequestProperty("User-Agent", "SmallBASIC"); |
807 | | - result.setRequestMethod(method); |
808 | | - result.setInstanceFollowRedirects(true); |
809 | | - if (apiKey != null && !apiKey.isEmpty()) { |
810 | | - result.setRequestProperty("Accept", "application/json"); |
811 | | - result.setRequestProperty("Content-Type", "application/json"); |
812 | | - result.setRequestProperty("Authorization", "Bearer " + apiKey); |
813 | | - } |
814 | | - result.setDoOutput(true); |
815 | | - return result; |
816 | | - } |
817 | | - |
818 | 771 | private Uri getSharedFile(File file) { |
819 | 772 | Uri result; |
820 | 773 | try { |
|
0 commit comments