mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2026-07-31 07:44:24 +02:00
Fix Bangle.js http requests through internet helper
This commit is contained in:
committed by
Arjan Schrijver
parent
1053767ec8
commit
4f3c521f91
+1
-1
@@ -93,7 +93,7 @@ class RebbleAppStoreActivity : AbstractGBActivity() {
|
||||
|
||||
private fun downloadInstallWatchappById(storeId: String) {
|
||||
val appUrl = "https://appstore-api.rebble.io/api/v1/apps/id/$storeId"
|
||||
val response: JSONObject? = InternetUtils.doRequest(appUrl.toUri())
|
||||
val response: JSONObject? = InternetUtils.doJsonRequest(appUrl.toUri())
|
||||
if (response != null) {
|
||||
val dataArray = response.getJSONArray("data")
|
||||
val firstAppObject = dataArray.getJSONObject(0)
|
||||
|
||||
+45
-3
@@ -62,6 +62,9 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.select.Elements;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -882,7 +885,8 @@ public class BangleJSDeviceSupport extends AbstractBTLESingleDeviceSupport {
|
||||
|
||||
String url = json.getString("url");
|
||||
final boolean insecure = json.optBoolean("insecure", false);
|
||||
String method = json.getString("method").toUpperCase(Locale.US);
|
||||
String method = "GET";
|
||||
if (json.has("method")) json.getString("method").toUpperCase(Locale.US);
|
||||
|
||||
String body = null;
|
||||
if (json.has("body"))
|
||||
@@ -904,9 +908,47 @@ public class BangleJSDeviceSupport extends AbstractBTLESingleDeviceSupport {
|
||||
}
|
||||
if (headers == null) headers = emptyMap();
|
||||
|
||||
JSONObject o = InternetUtils.Companion.doRequest(Uri.parse(url), method, headers, body, "application/json", insecure);
|
||||
uartTxJSON("http", o);
|
||||
String response = InternetUtils.Companion.doStringRequest(Uri.parse(url), method, headers, body, "application/json", insecure);
|
||||
JSONObject o = new JSONObject();
|
||||
String _xmlPath = "";
|
||||
String _xmlReturn = "";
|
||||
try {
|
||||
_xmlPath = json.getString("xpath");
|
||||
_xmlReturn = json.getString("return");
|
||||
} catch (JSONException ignored) {
|
||||
}
|
||||
final String xmlPath = _xmlPath;
|
||||
final String xmlReturn = _xmlReturn;
|
||||
if (!xmlPath.isEmpty()) {
|
||||
try {
|
||||
Document doc = Jsoup.parse(response);
|
||||
Elements result = doc.selectXpath(xmlPath);
|
||||
if (xmlReturn.equals("array")) {
|
||||
response = null; // don't add it below
|
||||
JSONArray arr = new JSONArray();
|
||||
for (int i = 0; i < result.size(); i++)
|
||||
arr.put(result.get(i).text());
|
||||
o.put("resp", arr);
|
||||
} else { // else return only first!
|
||||
response = "";
|
||||
if (!result.isEmpty())
|
||||
response = result.get(0).text();
|
||||
}
|
||||
} catch (Exception error) {
|
||||
uartTxJSONError("http", error.toString(), id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
try {
|
||||
o.put("t", "http");
|
||||
if( id!=null)
|
||||
o.put("id", id);
|
||||
if (response!=null)
|
||||
o.put("resp", response);
|
||||
} catch (JSONException e) {
|
||||
GB.toast(getContext(), "HTTP: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR, e);
|
||||
}
|
||||
uartTxJSON("http", o); }
|
||||
|
||||
/**
|
||||
* Handle "force_calendar_sync" packet
|
||||
|
||||
@@ -46,14 +46,14 @@ class InternetUtils {
|
||||
/**
|
||||
* Performs an HTTP request to the given URI, optionally allowing insecure connections.
|
||||
*/
|
||||
fun doRequest(
|
||||
fun doStringRequest(
|
||||
uri: Uri,
|
||||
method: String = "GET",
|
||||
requestHeaders: Map<String, String> = emptyMap(),
|
||||
body: String? = null,
|
||||
bodyContentType: String = "text/plain",
|
||||
insecure: Boolean = false
|
||||
): JSONObject? {
|
||||
): String? {
|
||||
val response: WebResourceResponse? = if (GBApplication.hasDirectInternetAccess()) {
|
||||
directRequest(uri, method, requestHeaders, body, bodyContentType, insecure)
|
||||
} else {
|
||||
@@ -62,8 +62,30 @@ class InternetUtils {
|
||||
if (response == null) return null
|
||||
|
||||
// Convert response InputStream to String
|
||||
val text = response.data.bufferedReader().use { it.readText() }
|
||||
return response.data.bufferedReader().use { it.readText() }
|
||||
}
|
||||
|
||||
fun doJsonRequest(
|
||||
uri: Uri,
|
||||
method: String = "GET",
|
||||
requestHeaders: Map<String, String> = emptyMap(),
|
||||
body: String? = null,
|
||||
bodyContentType: String = "text/plain",
|
||||
insecure: Boolean = false
|
||||
): JSONObject? {
|
||||
val text = doStringRequest(
|
||||
uri,
|
||||
method,
|
||||
requestHeaders,
|
||||
body,
|
||||
bodyContentType,
|
||||
insecure
|
||||
)
|
||||
try {
|
||||
return JSONObject(text)
|
||||
} catch (e: Exception) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
fun downloadBinaryFile(
|
||||
|
||||
Reference in New Issue
Block a user