Load local html in WebView?

前端 未结 5 469
悲&欢浪女
悲&欢浪女 2020-11-30 03:27

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\" ?

Secon

相关标签:
5条回答
  • 2020-11-30 04:12

    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);
    
    0 讨论(0)
  • 2020-11-30 04:20
    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);
        }
    
    0 讨论(0)
  • 2020-11-30 04:23

    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.

    0 讨论(0)
  • 2020-11-30 04:28

    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");
    
    0 讨论(0)
  • 2020-11-30 04:29

    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

    0 讨论(0)
提交回复
热议问题