1

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

主要活动

    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> 

我认为连接库的错误。 谢谢你的建议!

4

0 回答 0