[英]Load content to WebView
我想將此內容加載到WebView:
當我這樣嘗試時:
myWebView.loadUrl(myUrlAdress);
它正在加載內容,但是所有html格式可見,因此我可以看到所有的段落標簽和CSS樣式定義,因此它就像純文本。
當我這樣嘗試時:
myWebView.loadDataWithBaseURL(myUrlAdress, null, "text/html", "UTF-8", null);
根本沒有任何反應,webview保持空白,沒有任何數據。
我嘗試的最后一種方法是:
myWebView.loadDataWithBaseURL(myUrlAdress, myUrlAdress, "text/html", "UTF-8", null);
這次,webview只是從我嘗試獲取數據的鏈接中打印出鏈接,而沒有別的。
有人可以告訴我要走哪條路,這是什么問題?
問題在於服務器正在返回內容類型text / json,因此具有loadUrl的WebView不知道它應該解析並顯示HTML。 當您使用loadDataWithBaseUrl時,數據字符串必須是實際的HTML,而不僅僅是URL,這就是您所提供的。 如果您無法控制服務器的輸出,則需要將數據下載到一個字符串中,並將該字符串作為數據加載到Webview中,例如作為活動的內部類:
public class WebLoader extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
try {
final HttpClient client = new DefaultHttpClient();
final HttpGet request = new HttpGet(params[0]);
BasicResponseHandler handler = new BasicResponseHandler();
return client.execute(request, handler);
}
catch (Exception e) {
// do something
return null;
}
}
@Override
protected void onPostExecute(String html) {
webview.loadDataWithBaseURL(url, html, "text/html", "UTF-8", null);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.