webview android app无法加载任何图像或在网站链接中导航

时间:2017-11-25 06:33:32

标签: android webview android-webview

我正在尝试在Android应用网页视图中加载网站。

网站加载没有图片,网站上的所有图片都没有加载,我无法添加到购物车产品甚至打开其他产品的详细信息

MainActivity.java的代码如下所示。

 import android.app.ActionBar;
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.Window;
 import android.webkit.WebChromeClient;
 import android.webkit.WebSettings;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import android.widget.ShareActionProvider;
 public class MainActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.setWebChromeClient(new WebChromeClient());
    mWebView.setWebViewClient(new WebViewClient());
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.loadUrl("http://shopliek.com");
    mWebView.setWebViewClient(new MyAppWebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
 //hide loading image
            findViewById(R.id.progressBar1).setVisibility(View.GONE);
 //show webview

 findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
        }});
}
@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
 /** Inflating the current activity's menu with res/menu/items.xml */
    getMenuInflater().inflate(R.menu.menu_main, menu);
 /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();
 /** Setting a share intent */
    mShareActionProvider.setShareIntent(getDefaultShareIntent());
    return super.onCreateOptionsMenu(menu);
}
/** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "Make your Life Easier");
    intent.putExtra(Intent.EXTRA_TEXT," Visit google");
    return intent;
}
 }

以及此MyAppWebViewClient:

public class MyAppWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(Uri.parse(url).getHost().endsWith("www.shopliek.com")) {
        return false;
    }
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    view.getContext().startActivity(intent);
    return true;


}
}

我是初学者所以请用简单的指示和感谢解释

2 个答案:

答案 0 :(得分:0)

试试这个

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl("http://www.google.com");

希望这会奏效。如果有效,请告诉我。

答案 1 :(得分:0)

我使用了以下代码&一切正常:显示图片,详细信息页......

 public class WebviewActivity extends Activity {

    @BindView(R.id.webview)
    WebView web;

    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_webview);
            ButterKnife.bind(this);
            web.setWebViewClient(new myWebClient());
            web.getSettings().setJavaScriptEnabled(true);
            web.loadUrl(WebserviceUrl.BASE_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {

            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            progressBar.setVisibility(View.VISIBLE);
            web.loadUrl(url);

            return true;

        }

        public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
            final AlertDialog.Builder builder = new AlertDialog.Builder(WebviewActivity.this);
            builder.setMessage(R.string.notification_error_ssl_cert_invalid);
            builder.setPositiveButton(R.string.continue_to_page, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handler.proceed();
                }
            });
            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    handler.cancel();
                }
            });
            final AlertDialog dialog = builder.create();
            dialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
        }
    }


}

试试吧&让我知道结果