티스토리 뷰
Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.
Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.
아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.
How to pass html string to webview on android?
안드로이드에서 웹뷰에 html 문자열을 전달하는 방법은?
문제 내용
I am parsing xml and then loading it to web view. After parsing, I am creating four strings so that I could append all strings to one view. I am able to get two views on the web view, but not the first two strings.
xml을 구문 분석한 다음 웹뷰에 로드하고 있습니다. 구문 분석 후 모든 문자열을 하나의 뷰에 추가할 수 있도록 4개의 문자열을 만들고 있습니다. 나는 웹뷰에서 두 개의 뷰를 볼 수 있지만, 처음 두 개의 문자열은 볼 수 없었습니다.
Please suggest in my code, where I am going wrong and the correct way to get the formatted html strings on the web view.
내 코드에서 내가 어디서 잘못하고 있는지 그리고 웹 뷰에서 포맷된 html 문자열을 얻는 올바른 방법을 제안해 주세요.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String chapterTitle = "";
String SubChapterTitle="";
String chapterIntro ="";
String chapterContent="";
View view = convertView;
if (convertView == null) {
// view = inflater.inflate(resourceid, null);
view = getLayoutInflater().inflate(R.layout.webviewitem, null);
}
synchronized (view) {
WebView wv = (WebView) view.findViewById(R.id.contentWebView);
WebSettings settings = wv.getSettings();
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setJavaScriptEnabled(true);
settings.setDefaultZoom(ZoomDensity.FAR);
// wv.setBackgroundColor(0);
wv.setVerticalScrollBarEnabled(false);
wv.setHorizontalScrollBarEnabled(false);
/*String txtChapTitle = Intro.book.getsecretList().get(position)
.getChtitle().toString();*/
if (!(Intro.book.getsecretList().get(position).getChtitle()
.toString().equals(""))){
chapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
.getChtitle().toString()+"</font></b>";
}
if (!(Intro.book.getsecretList().get(position)
.getSubtitle() == null)) {
SubChapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
.getSubtitle().toString()+"</font></b>";
}
if (!(Intro.book.getsecretList().get(position)
.getIntro() == null)) {
chapterIntro = "<b><fontSize=2>"+Intro.book.getsecretList().get(position)
.getIntro().toString()+"</font></b>";
}
if (!(Intro.book.getsecretList().get(position)
.getContent() == null)) {
chapterContent = "<fontSize=2>"+Intro.book.getsecretList().get(position)
.getContent().toString()+"</font>";
}
StringBuilder content = new StringBuilder();
content.append(chapterTitle+SubChapterTitle+chapterIntro+chapterContent);
JsInterface Jsi = new JsInterface();
Jsi.wordDef = content ;
Log.v("Content", "" +content);
wv.addJavascriptInterface(Jsi, "interfaces");
wv.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.setHapticFeedbackEnabled(false);
}
});
wv.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url,
String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
wv.loadUrl("file:///android_asset/wordview.html");
}
return view;
}
}
I am able to get chapterIntro
and chaptercontent
on the web view, but not the first two strings.
웹뷰에서 chapterIntro 및 chaptercontent를 가져올 수 있지만 처음 두 문자열은 얻을 수 없었습니다.
높은 점수를 받은 Solution
i have successfully done by below line
나는 아래 선까지 성공적으로 해냈다.
//data == html data which you want to load
String data = "Your data which you want to load";
WebView webview = (WebView)this.findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadData(data, "text/html; charset=utf-8", "UTF-8");
Or You can try
아니면 시도해 볼 수 있어
webview.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
가장 최근 달린 Solution
I was using some buttons with some events, converted image file coming from server. Loading normal data wasn't working for me, converting into Base64 working just fine.
나는 몇 가지 이벤트와 함께 몇 개의 버튼을 사용하고 있었고, 서버에서 오는 변환된 이미지 파일을 사용했습니다. 정상적인 데이터를 로드하는 것은 나에게 효과가 없었고, Base64로 변환하는 것은 잘 작동했습니다.
String unencodedHtml ="<html><body>'%28' is the code for '('</body></html>";
tring encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING);
webView.loadData(encodedHtml, "text/html", "base64");
Find details on WebView
WebView에서 세부 정보 찾기
출처 : https://stackoverflow.com/questions/8987509/how-to-pass-html-string-to-webview-on-android
'개발 > 안드로이드' 카테고리의 다른 글
'This AsyncTask class should be static or leaks might occur' 경고 수정하기 (0) | 2022.12.08 |
---|---|
apk 설치 오류 수정하기 (0) | 2022.12.08 |
텍스트뷰 링크를 클릭할 수 있도록 하는 방법 (0) | 2022.12.07 |
'You need to use a Theme.AppCompat theme (or descendant) with this activity' 오류 수정하기 (0) | 2022.12.07 |
Android WebView 투명 배경 적용하기 (0) | 2022.12.07 |