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:///".

Jash Sayani
  • 1,241
  • 3
  • 12
  • 17

5 Answers5

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

  • 1
    @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: http://myexperiencewithandroid.blogspot.de/2011/09/android-loaddatawithbaseurl.html ... you should also read this: http://en.wikipedia.org/wiki/Character_encoding ... so i do not see your problem. –  Jan 01 '13 at 18:11
  • 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." –  Jan 02 '13 at 11:54
  • 1
    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). – quietmint Jan 03 '13 at 01:48
14
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);
    }
Viren Savaliya
  • 520
  • 1
  • 6
  • 18
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);
Alican Temel
  • 1,318
  • 13
  • 13
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.

sahhhm
  • 5,325
  • 2
  • 27
  • 22
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");
JJD
  • 50,076
  • 60
  • 203
  • 339
Dev.Sinto
  • 6,802
  • 8
  • 36
  • 54