2

我有一个 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 到 assets 文件夹,然后加载保存的 HTML 工作正常。但是为什么我不能从网络上加载它?

4

1 回答 1

1

这样做

private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

然后像这样使用您的网络视图设置网络视图客户端

 web.setWebViewClient(new HelloWebViewClient());
于 2013-11-21T07:57:00.760 回答