android本地服务器加载html,android-在WebView中加载本地html?

android-在WebView中加载本地html?

我想使用“ [file:///”]将本地html加载到WebView中而不使用,因为这不允许cookie。 有没有办法使用“ localhost”之类的东西?

其次,我找不到在getSettings()中启用cookie的方法。 因为使用“ [file:///”时不允许使用cookie。]

Jash Sayani asked 2020-08-03T20:02:51Z

5个解决方案

100 votes

您只能这样做。 此解决方案从String变量加载HTML:

String html = "

Hello, World!";

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);

编辑:尝试根据您的需要设置loadDataWithBaseURL()的第一个参数(baseURL)

answered 2020-08-03T20:03:10Z

14 votes

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 answered 2020-08-03T20:03:25Z

9 votes

试试这个代码。 这个对我有用。

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 answered 2020-08-03T20:03:45Z

4 votes

如果要通过Android访问localhost,则需要使用http://10.0.2.2:35643/,其中35643是特定端口(如果需要)。

sahhhm answered 2020-08-03T20:04:05Z

0 votes

以下代码为我工作。

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");

DroidBot answered 2020-08-03T20:04:25Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值