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:///".
You can only do something like that. This solution load HTML from a String variable:
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);
EDIT: try to set the first parameter (the baseURL) of loadDataWithBaseURL() for your needs
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);
}
Try this code. It works for me.
WebSettings settings = mDesc.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mDesc.loadData(mDescText, "text/html; charset=utf-8",null);
up vote
24
down vote
favorite
6 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:///".
android webview
share|improve this question asked Dec 28 '10 at 2:55
Jash Sayani
4731615
Please accept the answer if it helped you. – Sufian Nov 1 '13 at 10:57
add a comment |
5 Answers 5 active
oldest
votes
up vote
63
down vote You can only do something like that. This solution load HTML from a String variable:
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);
EDIT: try to set the first parameter (the baseURL) of loadDataWithBaseURL() for your needs
share|improve this answer edited Dec 28 '10 at 3:41
answered Dec 28 '10 at 3:36
user432219
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: myexperiencewithandroid.blogspot.de/2011/09/… ... you should also read this: en.wikipedia.org/wiki/Character_encoding ... so i do not see your problem. – user432219 Jan 1 '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." – user432219 Jan 2 '13 at 11:54
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). – user113215 Jan 3 '13 at 1:48
add a comment |
up vote
9
down vote 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);
}
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;
}
Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology