无法使用addJavascriptInterface将数据从JS传输到Webview

时间:2017-05-05 03:10:39

标签: javascript android webview

我想在android webview中输入一个网页。同时,服务器会向我传输一些数据。 在JS中:



function callAndroidApp(){
		       try {
			        wv.PostDataFromWebToWebView('".$msg."');
			    } catch(err) {
			    	console.log('The android context does not exist yet');
			    }
			}
callAndroidApp();



 在java中:

public class webview extends AppCompatActivity {

@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);
    WebView webView=(WebView) findViewById(R.id.web_view);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(this,"wv" );
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);  //关键点
    webSettings.setBuiltInZoomControls(true); // 设置显示缩放按钮
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("http://health.sjtu.edu.cn");
    webView.requestFocusFromTouch();       //支持获取手势焦点

    Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    String ua=webView.getSettings().getUserAgentString();
    webView.getSettings().setUserAgentString(ua+";sjtu-android");

}
@JavascriptInterface
public void PostDataFromWebToWebView(String msg){
    parseJSONWithJSONObject(msg);
}
public void parseJSONWithJSONObject(String msg2){
    try{
        JSONObject jsonData=new JSONObject(msg2);
        String userid=jsonData.getString("userid");
        String action=jsonData.getString("action");
        String name=jsonData.getString("name");
        String jaccount=jsonData.getString("jaccount");
        String token=jsonData.getString("token");
        URL url=new URL("https://health.sjtu.edu.cn/post-data-from-app.php?jaccount="+jaccount+"&userid="+userid+"&token="+token+"&CMD=current_steps&steps="+1234);
        Log.v("webhttpsrequest",url.toString());
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

有时尤其是第一次,它有效。但总是失败。 Logcat显示如下:

05-05 10:43:13.041 10871-10909 / com.step.pedometer.mystep E / OpenGLRenderer:PerfServiceNative_notifyRenderTime init失败! 05-05 10:43:23.026 10871-10871 / com.step.pedometer.mystep E / SysUtils:ApplicationStatus中的ApplicationContext为null 05-05 10:43:23.048 10871-10871 / com.step.pedometer.mystep E / libEGL:validate_display:255错误3008(EGL_BAD_DISPLAY) 05-05 10:43:23.048 10871-10871 / com.step.pedometer.mystep E / libEGL:validate_display:255错误3008(EGL_BAD_DISPLAY) 05-05 10:43:23.078 10871-10871 / com.step.pedometer.mystep E / DataReductionProxySettingListener:由于异常而没有DRP密钥:java.lang.ClassNotFoundException:com.android.webview.chromium.Drp 05-05 10:43:23.093 10871-10871 / com.step.pedometer.mystep E / MPlugin:不支持的类:com.mediatek.common.telephony.IOnlyOwnerSimSupport 05-05 10:43:23.247 10871-10909 / com.step.pedometer.mystep E / Surface:getSlotFromBufferLocked:未知缓冲区:0x7f82891200 05-05 10:43:25.402 10871-10871 / com.step.pedometer.mystep E / chromium:[错误:aw_autofill_client.cc(175)]未在虚拟空中实现android_webview :: AwAutofillClient :: OnFirstUserGestureObserved() 05-05 10:43:28.199 10871-10909 / com.step.pedometer.mystep E / Surface:getSlotFromBufferLocked:未知缓冲区:0x7f7465c340 05-05 10:43:31.467 10871-10909 / com.step.pedometer.mystep E / Surface:getSlotFromBufferLocked:未知缓冲区:0x7f7465c340 05-05 10:56:56.360 10871-10909 / com.step.pedometer.mystep E / Surface:getSlotFromBufferLocked:未知缓冲区:0x7f74659140

0 个答案:

没有答案