我有一个WebView,我使用webView.loadUrl("http://myurl.com")
加载页面,并且该页面在Android 4.X 上显示正确但在尝试 2.X 时,更具体地说,2.3.5显示与此
可能是什么问题以及如何解决?谢谢!
我的完整代码:
public class FragmentPolicy extends SherlockFragment implements Refreshable {
private WebView webView;
private Bundle webViewBundle;
private UpdateReceiver updateReceiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
updateReceiver = new UpdateReceiver();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.fragment_policy, container, false);
webView = (WebView) ll.findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.startsWith("mailto:")){
MailTo mt = MailTo.parse(url);
Intent i = newEmailIntent(getSherlockActivity(), mt.getTo(), mt.getSubject(), mt.getBody(), mt.getCc());
startActivity(i);
view.reload();
return true;
} else {
view.loadUrl(url);
}
return true;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.stopLoading();
webView.clearView();
Log.w("test", "Could not get policy: " + description + " Errror code: " + errorCode + ". Url: " + failingUrl);
}
});
loadPolicy();
return ll;
}
private void loadPolicy(){
if (webViewBundle == null) {
Map<String, String> headers = new HashMap<String, String>();
//does not work with webview
headers.put("Content-Type", "text/html; charset=utf-8");
headers.put("Accept", "text/html");
webView.loadUrl(Utils.getBaseAPIAddr()+"getpolicyhtml", headers);
//webView.loadData(URLEncoder.encode(result).replaceAll("\\+", " "), "text/html", Encoding.UTF_8.toString());
} else {
webView.restoreState(webViewBundle);
}
}
private Intent newEmailIntent(Context context, String address, String subject, String body, String cc) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {address});
intent.putExtra(Intent.EXTRA_TEXT, body);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_CC, cc);
intent.setType("message/rfc822");
return intent;
}
@Override
public void onPause() {
super.onPause();
webViewBundle = new Bundle();
webView.saveState(webViewBundle);
}
@Override
public void onResume(){
super.onResume();
LocalBroadcastManager.getInstance(getSherlockActivity()).registerReceiver(updateReceiver, new IntentFilter("update"));
}
@Override
public void onStop(){
super.onStop();
if(updateReceiver != null){
LocalBroadcastManager.getInstance(getSherlockActivity()).unregisterReceiver(updateReceiver);
}
}
@Override
public void refresh(){
loadPolicy();
}
public class UpdateReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
loadPolicy();
}
}
}
我尝试过的事情
*通过手动将HTML复制并粘贴到资源文件夹,将HTML保存到资源文件夹,然后加载保存的HTML工作正常。但为什么我不能从网上加载它?
答案 0 :(得分:1)
这样做
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
然后使用您的网络视图设置webview客户端
web.setWebViewClient(new HelloWebViewClient());