gpt4 book ai didi

java - 如何从 webview 获取 javascript 值到 android?

转载 作者:太空宇宙 更新时间:2023-11-04 13:57:15 25 4
gpt4 key购买 nike

如果我的 web View 中存在类“logout”,我会尝试获取 0 或 1 的值。 (基本思想是,如果我找到“注销”类,则意味着用户已登录,这就是我需要知道的)

到目前为止,我已经搜索并得到了类似的结果。

JavscriptInterface() 类

private static class JavascriptInterface{
private Map<String,String> valueMap = new HashMap<String,String>();
public String set(String key, String value){
valueMap.put(key, value);
return "";
}

public String get(String key){
return valueMap.get(key);
}
}

在onCreate()中

webview_mycare = (WebView) findViewById(R.id.webview_mycare);
webview_mycare.getSettings().setJavaScriptEnabled(true);
js = new JavascriptInterface();
webview_mycare.addJavascriptInterface(js, "jsinterface");
js.set(JS_KEY, "-1");

webview_mycare.loadUrl(
"javascript:document.function hasClass(element, cls){ return(' '+ element.className +' ').indexOf(' '+ cls +' ')>-1;"
+ "javascript:document.function var el = document.getElementsByClassName('front-page');"
+ "javascript:document.function if(hasClass(el,'logout')==true){ "
+ "window.jsinterface.set('"+JS_KEY+"','1');"
+ "}else{"
+ "window.jsinterface.set('"+JS_KEY+"','0');"
+ "}"
);
try{
Thread.sleep(200);
}catch(InterruptedException ex){

}

String v=js.get(JS_KEY);
System.out.println("isMapPage: getValue: value="+v);
if("1".equals(v)){
Log.e(">>>>>>>>>>>>", "JS_KEY "+ v);
}else{
Log.e(">>>>>>>>>>>>", "JS_KEY "+ v);
}

webview_mycare.loadUrl(URL);

URL 是带有我的网址的字符串

到目前为止结果是-1。我写错了什么?

最佳答案

试试这个:

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.annotation.SuppressLint;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener {

private static final String URL = "file:///android_asset/index.html";
private WebView mWebView;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
String user = ((EditText) findViewById(R.id.edit_text)).getText().toString();
if (user.isEmpty()) {
user = "World";
}
String javascript="javascript: document.getElementById('msg').innerHTML='Hello "+user+"!';";
view.loadUrl(javascript);
}
});
refreshWebView();
findViewById(R.id.button).setOnClickListener(this);
}

private void refreshWebView() {
mWebView.loadUrl(URL);
}

@Override
public void onClick(View v) {
refreshWebView();
}

}

还有这个 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edit_text"
android:hint="What is your name?" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="Update Web View"/>

<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:id="@+id/webview" />

</LinearLayout>

关于java - 如何从 webview 获取 javascript 值到 android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29694865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com