JavaScript在webView中不起作用

时间:2013-04-08 13:19:06

标签: javascript android webview

我在使用WebView时遇到了一些问题。 Javascript不起作用。在android 2.2 whith loadUrl()上,它运行良好。但是当我使用 loadDataWithBaseURL 在android 4上运行它时,它不起作用。我做错了什么?

MainActivity

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends Activity {

    String webData = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webData = getHtmlFromAsset();

        WebView webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        //webView.loadUrl("file:///android_asset/index.html");
        //webView.loadData(webData, "text/html", "UTF-8");

        webView.addJavascriptInterface(new JavaScriptIntefeise(this), "Android");
        webView.loadDataWithBaseURL(getAssets().toString(), webData, "text/html", "UTF-8", null);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private String getHtmlFromAsset() {
        InputStream is;
        StringBuilder builder = new StringBuilder();
        String htmlString = null;
        try {
            is = getAssets().open(getString(R.string.index));
            if (is != null) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }

                htmlString = builder.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return htmlString;
    }

    class JavaScriptIntefeise {

        Context ctx;

        public JavaScriptIntefeise(Context ctx) {
            this.ctx = ctx;
        }

        public void showToast() {
            Toast.makeText(ctx, "ping", 3000).show();
        }
    }
}

简单的sgnalR客户端 index.html

<html>
<head>
<title>Insert title here</title>
<script type="text/javascript" src="json2.js" ></script>
<script type="text/javascript" src="jquery-1.9.0.js" ></script>
<script type="text/javascript" src="jquery.signalR.js" ></script>
<script type="text/javascript"         src="http://192.168.0.100/FloorSpy.DuplexService/signalr/hubs/"></script>

</head>
<body>
<span>signalR</span>

<script type="text/javascript">
if(!window.jQuery) document.body.style.backgroundColor = '#ff8000';;


$.connection.hub.url = "http://192.168.0.100/FloorSpy.DuplexService/signalr";
var connection = $.connection.notif;

connection.client.pong = function(data) {
    Android.showToast();
};

$.connection.hub.start()
.done(function() {
  // Android.showToast();
})

.fail(function(data) {
    //Android.showToast();
});


</script>
</body>
</html> 

我认为连接库时出错。 感谢您的建议!

0 个答案:

没有答案