Vote count:
0
Payment module is in my app which is done in webview. The problem is when payment process gets complete, a JSON is returned which has html content to load into the webview. I intercepted the json and parsed it through javascript. But, instead of HTML, whole json appears in the webview.
Here's my code:
webVw_payments.getSettings().setJavaScriptEnabled(true);
webVw_payments.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (paymentType == PaymentType.PAYMENT_BUY_FLOW_BEST_OFFERS) {
view.loadUrl("javascript:window.injectedObject.processContent(document.getElementsByTagName('body')[0].innerText);");
}
}
});
webVw_payments.addJavascriptInterface(new JsObject(webVw_payments_1),
"injectedObject");
class JsObject {
WebView webView;
public JsObject(WebView webView) {
this.webView = webView;
}
@JavascriptInterface
public void processContent(String content) {
if (content.startsWith("{")) {
try {
JSONObject jsonObject = new JSONObject(content.trim());
String responseCode = jsonObject.optString("code");
if (responseCode.equalsIgnoreCase("success")) {
JSONObject data = jsonObject.optJSONObject("data");
final String html = data.optString("html");
order_id = data.optString("order_id");
webView.post(new Runnable()
{
public void run()
{
webView.loadData(html, "text/html", "UTF-8");
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
} else
webView.loadData(content, "text/html", "UTF-8");
}
}
asked 1 min ago
JSON gets loaded in webview instead of HTML content
Aucun commentaire:
Enregistrer un commentaire