44

I want to load a local html into a WebView WITHOUT using "file:///" because that does not allow cookies. Is there a way to use something like "localhost" ?

Secondly, I could not find a way to enable cookies in the getSettings(). Because cookies are not allowed while using "file:///".

0

5 Answers 5

102

You can only do something like that. This solution load HTML from a String variable:

String html = "<html><body>Hello, World!</body></html>";
String mime = "text/html";
String encoding = "utf-8";

WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);

EDIT: try to set the first parameter (the baseURL) of loadDataWithBaseURL() for your needs

Sign up to request clarification or add additional context in comments.

3 Comments

@user113215: but you have read the documentation link for loadDataWithBaseURL() method, haven't you?? the 4th argument is called "encoding", so i called the variable "encoding" ... as you can see here it is used for the output charset: myexperiencewithandroid.blogspot.de/2011/09/… ... you should also read this: en.wikipedia.org/wiki/Character_encoding ... so i do not see your problem.
but no (data) scheme is used here in the example, only local HTML data is loaded that should be displayed in the WebView: "If the base URL uses the data scheme, this method is equivalent to calling loadData() and the historyUrl is ignored."
Ah, this is confusing. I investigated the Android source and you are correct. Using this method with the data: URL scheme results in a call to nativeLoadUrl() (where encoding denotes either Base64 or URL encoding), but otherwise this method results in a call to nativeLoadData() (where encoding denotes character set).
15
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView view = (WebView) findViewById(R.id.webView1);
        try {
        InputStream input = getResources().openRawResource(R.raw.lights);
        Reader is = new BufferedReader(
                new InputStreamReader(input, "windows-1252"));


            //InputStream input = getAssets().open("ws.TXT");
            int size;
            size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            // byte buffer into a string
            javascrips = new String(buffer);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // String html = readFile(is);

        view.loadDataWithBaseURL("file:///android_res/raw/", javascrips, "text/html",
                "UTF-8", null);
    }

4 Comments

Nice snippet of code, just missing the definition ` String javascrips;`.
javascrips is any html files text. like <html><body> ... </html>
I'm taking this the extra step. webview.loadUrl("file:///android_res/raw/help.html");
view.loadDataWithBaseURL("file:///android_res/raw/" is provide local images path, where webpage create from text (javascrips string) and image required path where it located.
9

Try this code. It works for me.

WebView mDesc = findViewById(R.id.descWv);
WebSettings settings = mDesc.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mDesc.loadData(mDescText, "text/html; charset=utf-8",null);

2 Comments

What is mDesc? This answer is useless without it.
@Supuhstar Thanks for your attention. "mDesc" is webview. I edited my code.
4

If you want to access localhost through the Android, you need to use http://10.0.2.2:35643/ where 35643 is the specific port, if needed.

Comments

0

Following code worked for me.

String base64EncodedString = null;
try {
    base64EncodedString = android.util.Base64.encodeToString(
        (preString+mailContent.getBody() + postString).getBytes("UTF-8"), 
        android.util.Base64.DEFAULT);
} catch (UnsupportedEncodingException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
if (base64EncodedString != null)
{
    wvMailContent.loadData(base64EncodedString, "text/html; charset=utf-8", "base64");  
}
else
{
wvMailContent.loadData(preString+mailContent.getBody() + postString, "text/html; charset=utf-8", "utf-8");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.