mirror of
https://github.com/status-im/react-native-webview-bridge.git
synced 2026-08-30 21:01:12 +00:00
async StatusBridge.sendRequest
This commit is contained in:
@@ -3,6 +3,7 @@ package com.github.alinz.reactnativewebviewbridge;
|
||||
import android.webkit.JavascriptInterface;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.Toast;
|
||||
import android.app.Activity;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -12,30 +13,52 @@ import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
import java.io.StringReader;
|
||||
import android.util.JsonReader;
|
||||
import com.facebook.react.uimanager.ThemedReactContext;
|
||||
|
||||
class StatusBridge {
|
||||
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
public static final String URL = "http://localhost:8545";
|
||||
|
||||
private WebView webView;
|
||||
private Context context;
|
||||
private ThemedReactContext context;
|
||||
private OkHttpClient client = new OkHttpClient();
|
||||
|
||||
public StatusBridge(Context context, WebView webView) {
|
||||
public StatusBridge(ThemedReactContext context, WebView webView) {
|
||||
this.context = context;
|
||||
this.webView = webView;
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public String sendRequest(String json) {
|
||||
RequestBody body = RequestBody.create(JSON, json);
|
||||
Request request = new Request.Builder()
|
||||
.url(URL)
|
||||
.post(json)
|
||||
.build();
|
||||
public void sendRequest(final String callbackId, final String json) {
|
||||
Thread thread = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
RequestBody body = RequestBody.create(JSON, json);
|
||||
Request request = new Request.Builder()
|
||||
.url(URL)
|
||||
.post(body)
|
||||
.build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
Toast.makeText(context, response.body().string(), Toast.LENGTH_LONG).show();
|
||||
return response.body().string();
|
||||
}
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
//Toast.makeText(context, response.body().string(), Toast.LENGTH_LONG).show();
|
||||
String rpcResponse = response.body().string().trim();
|
||||
|
||||
final String script = "httpCallback('" + callbackId + "','" + rpcResponse + "');";
|
||||
final Activity activity = context.getCurrentActivity();
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
webView.evaluateJavascript(script, null);
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
+2
-18
@@ -10,6 +10,7 @@ import android.text.TextUtils;
|
||||
import android.view.ViewGroup.LayoutParams;
|
||||
import android.webkit.*;
|
||||
import android.net.http.SslError;
|
||||
import android.app.Activity;
|
||||
|
||||
import com.facebook.common.logging.FLog;
|
||||
import com.facebook.react.common.ReactConstants;
|
||||
@@ -120,7 +121,7 @@ public class WebViewBridgeManager extends ReactWebViewManager {
|
||||
|
||||
webView.setWebChromeClient(new ReactWebChromeClient());
|
||||
webView.addJavascriptInterface(new JavascriptBridge(webView), "WebViewBridge");
|
||||
wevView.addJavascriptInterface(new StatusBridge(reactContext, webView), "StatusBridge");
|
||||
webView.addJavascriptInterface(new StatusBridge(reactContext, webView), "StatusBridge");
|
||||
|
||||
return webView;
|
||||
}
|
||||
@@ -413,23 +414,6 @@ public class WebViewBridgeManager extends ReactWebViewManager {
|
||||
event.putBoolean("canGoForward", webView.canGoForward());
|
||||
return event;
|
||||
}
|
||||
|
||||
String gethHost1 = "localhost";
|
||||
String gethHost2 = "127.0.0.1";
|
||||
|
||||
@Override
|
||||
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
|
||||
|
||||
try {
|
||||
URL url = new URL(error.getUrl());
|
||||
String host = url.getHost();
|
||||
if(url.getPort() == 8545 && (host.equals(gethHost1) || host.equals(gethHost2))) {
|
||||
handler.proceed();
|
||||
}
|
||||
} catch (MalformedURLException e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user