qwerty12345
qwerty12345

Reputation: 265

Why my WebView doesn't work properly, wrong rendering?

I almost give up with that. I do not know what I am doing wrong that my HTML code doesn't rendered and I get error that webpage not found file:///..... I try to do that based on Problems loading html asset into webview + Here my code:

Main java class MainActivity.java

package com.example.webviewvulnerability;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button button;




public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final String URL="file:///home/m.sepczuk/Downloads/eclipse/workspace/WebViewVulnerability/assets/index.html";
    final WebView webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new WebViewClient());
    webview.loadUrl(URL);

}

Manifest file Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webviewvulnerability"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.webviewvulnerability.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

index file in /assets directory/assets/index.html

index
<!doctype html>
<html>
<head>
</head>
<body>
    <center>
        <div id="msg">It works!!!</div>
    </center>
</body>
</html>

Upvotes: 1

Views: 1143

Answers (3)

Joe Richard
Joe Richard

Reputation: 1520

package com.example.webviewvulnerability;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    private Button button;

    @SuppressLint("SetJavaScriptEnabled")
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //URL for html file in assets should look like this
        final String URL = "file:///android_asset/index.html";
        final WebView webview = new WebView(this);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl(URL);

        //Reference to your layout and then add webview to it
        LinearLayout llMain=(LinearLayout) findViewById(R.id.llMain);
        llMain.addView(webview);
    }
}

Upvotes: 0

Luciano
Luciano

Reputation: 2798

First of all your Webview is not used anywhere.. Create a Webview in your main.xml then init it by calling:

  Webview webview = (Webview) findViewById(R.id.yourwebviewid); 

And i think your url isnt correct, you should use something like:

  webview.loadUrl("file:///android_asset/index.html");

Upvotes: 0

Illegal Argument
Illegal Argument

Reputation: 10338

You are providing wrong url to webview. The url should be something like this:

wv.loadUrl("file:///android_asset/index.html");

Upvotes: 2

Related Questions