2016-03-31 48 views
0

我想用JS腳本將下面的html代碼運行到webview中。如何在WebView上使用JS腳本運行html

<html> 
    <script type="text/javascript" src="//static.apester.com/js/sdk/v1.1/apester-sdk.min.js"></script> 
    <body> 
     <interaction id="56fda67582287b5830c4ec8f"></interaction> 
    </body> 
</html> 

上面的代碼運行在Tryit Editor

我已經加入了INTERNET權限清單

XML代碼(沒有新的):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="bet.abpla.webviewdemo02.MainActivity"> 

    <WebView 
     android:id="@+id/webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

Java代碼:

public class MainActivity extends AppCompatActivity { 

    String Identifier = "56fda67582287b5830c4ec8f"; 
    String Mime = "text/html"; 
    String Encoding = "UTF-8"; 

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

     WebView webView = (WebView) findViewById(R.id.webview); 
     webView.getSettings().setJavaScriptEnabled(true); 
     webView.loadDataWithBaseURL(null, getHTMLData(Identifier), Mime, Encoding, null); 
    } 

    private String getHTMLData(String ApesterId) { 

     return "<!DOCTYPE html>" + 
       "<html>" + 
       "<script type=\"text/javascript\" src=\"//static.apester.com/js/sdk/v1.1/apester-sdk.min.js\"></script>" + 
       "<body>" + 
       "<interaction id=\"" + ApesterId + "\"></interaction>" + 
       "</body>" + 
       "</html>"; 
    } 

} 

粘貼上Tryit Editor上述HTML代碼將顯示預期的結果。

不幸的是,在android中,它什麼也沒有顯示(只是一個空白的白色屏幕,沒有Hello World文本的默認操作欄)。有人能告訴我我錯了哪裏?

問候

回答

0

改變你的代碼的最後一行:

webview.loadDataWithBaseURL("", getHTMLData(Identifier), "text/html", "UTF-8", "");