Android WebView不加载URL?

时间:2022-09-04 08:06:37

I have an Android WebView which should load the following URLs:

我有一个Android WebView,应该加载以下url:

https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655

https://buchung.salonmeister.de/place/#offer-details-page?id=907599&venueId=301655

Here is what I did:

以下是我所做的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/holo_orange_light"
                tools:context=".MainActivityFragment">



    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="5dp"
        android:layout_above="@+id/progressBar"></WebView>


    <ProgressBar
        android:id="@id/progressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:visibility="invisible"
        style="?android:attr/progressBarStyleHorizontal"/>

    <TextView
        android:id="@+id/titleView"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</RelativeLayout>

In onCreateView():

在onCreateView():

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
       View v = inflater.inflate(R.layout.fragment_main, container, false);

        String url = // some url
        final ProgressBar progressBar = (ProgressBar)v.findViewById(R.id.progressBar);
        progressBar.setMax(100);

        final TextView titleView = (TextView)v.findViewById(R.id.titleView);


        mWebView = (WebView)v.findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAllowContentAccess(true);

        mWebView.setWebViewClient(new WebViewClient() {
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
          }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
          @Override
          public void onProgressChanged(WebView view, int progress) {
            if (progress == 100) {
              progressBar.setVisibility(View.INVISIBLE);
              progressBar.setProgress(0);
            } else {
              progressBar.setVisibility(View.VISIBLE);
              progressBar.setProgress(progress);
            }
          }

          @Override
          public void onReceivedTitle(WebView view, String title) {
            titleView.setText(title);
          }
        });
        mWebView.loadUrl(url);

        return v;
}

The screen is empty when I start my activity.

当我开始活动时,屏幕是空的。

Note: This is not a layout issue. Please do not comment on layout and focus on the real problem.

注意:这不是布局问题。请不要评论布局,关注真正的问题。

How can I load the above urls in a WebView?

如何在WebView中加载上述url ?

Edit: Here are the permissions I gave:

编辑:以下是我授予的权限:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Edit:

编辑:

In my console I get the following:

在我的控制台,我得到以下信息:

I/chromium: [INFO:CONSOLE(84)] "undefined", source: https://buchung.salonmeister.de/place/ (84)

09-13 15:12:33.230 28407-28407/de.test.webviewtest I/chromium: [INFO:CONSOLE(1)] "Uncaught Error: Script error for: widget/venue-menu/mobile/venue-menu-app
09-13 15:12:33.230 28407-28407/de.test.webviewtest I/chromium: http://requirejs.org/docs/errors.html#scripterror", source: https://buchung.salonmeister.de/javascript-module/1.162.3/thirdparty/require.2.1.10.js (1)

6 个解决方案

#1


7  

The first problem is you have to swap the position of ProgressBar and WebView.

第一个问题是您必须交换ProgressBar和WebView的位置。

<ProgressBar
    android:id="@id/progressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:visibility="invisible"
    style="?android:attr/progressBarStyleHorizontal"/>

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="5dp"
    android:layout_above="@id/progressBar"/> 

The second problem is this line:

第二个问题是这条线:

android:layout_above="@+id/progressBar"

Inside a RelativeLayout, you must have the view id declared in advance, so that another view can be layouted above(right, left is the same way) to.

在RelativeLayout中,必须预先声明视图id,以便在上面(右、左是相同的方式)对另一个视图进行layouted。

And again is the same line:

同样的一句话是:

android:layout_above="@+id/progressBar"

Inside a RelativeView, a view can not layout above(or to below, to left to right) to an ID which is generated for nothing.

在RelativeView中,一个视图不能将不需要任何代价生成的ID放在上面(或下面,从左到右)。

You do

你做

@+id/progressBar

the IDE will auto-generate a new ID, this ID is not the same ID of you mean the WebView to layout above to. And in your ProgressBar declaration, you @+id/progressBar again, which it will re-generate another id.

IDE将自动生成一个新的ID,这个ID与你所说的WebView的ID不同。在ProgressBar声明中,您再次@+id/ ProgressBar,它将重新生成另一个id。

So, your WebView is layouted above to nothing, you can not even see the WebView, it may loads the URLs, and you just can't see it.

所以,你的WebView被过滤到上面什么都没有,你甚至看不到WebView,它可能会加载url,你就是看不到它。

Good luck.

祝你好运。

#2


3  

UPDATE: with setJavaScriptEnabled(false);, the Url "https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655" can displayed successfully in the WebView.

更新:使用setjavastenabled (false);, Url“https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655”可以在WebView中成功显示。

Android WebView不加载URL?

public class WebViewActivityFragment extends Fragment {

    public WebViewActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //return inflater.inflate(R.layout.fragment_web_view, container, false);

        View v = inflater.inflate(R.layout.fragment_web_view, container, false);

        final String url = "https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655";//"https://google.com.vn";
        final ProgressBar progressBar = (ProgressBar)v.findViewById(R.id.progressBar);
        progressBar.setMax(100);
        final TextView titleView = (TextView)v.findViewById(R.id.titleView);
        WebView mWebView = (WebView) v.findViewById(R.id.webView);
        mWebView.setInitialScale(1);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setJavaScriptEnabled(false);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAllowContentAccess(true);
        mWebView.setScrollbarFadingEnabled(false);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                if (progress == 100) {
                    progressBar.setVisibility(View.INVISIBLE);
                    progressBar.setProgress(0);
                } else {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(progress);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                titleView.setText(title);
            }


        });
        mWebView.loadUrl(url);

        return v;
    }
}

I have used your entire code, here my web view result. I think perhaps that website is not mobile-ready because I tried to open your 2 urls in web browser in my phone the same problem (still loading...)

我已经使用了你的全部代码,这是我的web view结果。我想这个网站可能不适合移动,因为我试着在我的手机里打开你的两个网址同样的问题(仍然在加载…)

Android WebView不加载URL?

#3


3  

if you want you can try to use Indeterminate Progress Bar Example and remove setWebChromeClient try this code :

如果你想要,你可以尝试使用不确定的进度条示例并删除setWebChromeClient试试以下代码:

public class WebViewActivityFragment extends Fragment {

 public WebViewActivityFragment() {
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //return inflater.inflate(R.layout.fragment_web_view, container, false);

    View v = inflater.inflate(R.layout.fragment_web_view, container, false);

    final String url = "https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655";//"https://google.com.vn";
    final ProgressBar progressBar = (ProgressBar)v.findViewById(R.id.progressBar);
    progressBar.setMax(100);
    progressBar.setVisibility(View.GONE);
    final TextView titleView = (TextView)v.findViewById(R.id.titleView);
    WebView mWebView = (WebView) v.findViewById(R.id.webView);
    mWebView.setInitialScale(1);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setJavaScriptEnabled(false);
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setAllowContentAccess(true);
    mWebView.setScrollbarFadingEnabled(false);
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

         @Override
        public void onPageFinished(WebView view, String url) {
         progressBar.setVisibility(View.GONE);
         progressBar.setProgress(100);
         super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
         progressBar.setVisibility(View.VISIBLE);
         progressBar.setProgress(0);
         super.onPageStarted(view, url, favicon);
        }
    });


    mWebView.loadUrl(url);

    return v;
}

}

}

#4


2  

Your URL is https so It's ssl problem.

你的URL是https,所以这是ssl问题。

You can find a solution in other question.

你可以在其他问题中找到答案。

mWebView.setWebViewClient(new WebViewClient() {

    @Override
    public boolean shouldOverride UrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed(); // Ignore SSL certificate errors
    }
});

#5


0  

Make sure you to put all the permissions in menifest file outside of application tag.

确保您将所有权限放在应用程序标记之外的menifest文件中。

#6


0  

This may solve your issue :

这可能会解决你的问题:

webView.post(new Runnable() {
    @Override
    public void run() {
        webView.loadUrl(url);
    }
});

#1


7  

The first problem is you have to swap the position of ProgressBar and WebView.

第一个问题是您必须交换ProgressBar和WebView的位置。

<ProgressBar
    android:id="@id/progressBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:visibility="invisible"
    style="?android:attr/progressBarStyleHorizontal"/>

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:layout_margin="5dp"
    android:layout_above="@id/progressBar"/> 

The second problem is this line:

第二个问题是这条线:

android:layout_above="@+id/progressBar"

Inside a RelativeLayout, you must have the view id declared in advance, so that another view can be layouted above(right, left is the same way) to.

在RelativeLayout中,必须预先声明视图id,以便在上面(右、左是相同的方式)对另一个视图进行layouted。

And again is the same line:

同样的一句话是:

android:layout_above="@+id/progressBar"

Inside a RelativeView, a view can not layout above(or to below, to left to right) to an ID which is generated for nothing.

在RelativeView中,一个视图不能将不需要任何代价生成的ID放在上面(或下面,从左到右)。

You do

你做

@+id/progressBar

the IDE will auto-generate a new ID, this ID is not the same ID of you mean the WebView to layout above to. And in your ProgressBar declaration, you @+id/progressBar again, which it will re-generate another id.

IDE将自动生成一个新的ID,这个ID与你所说的WebView的ID不同。在ProgressBar声明中,您再次@+id/ ProgressBar,它将重新生成另一个id。

So, your WebView is layouted above to nothing, you can not even see the WebView, it may loads the URLs, and you just can't see it.

所以,你的WebView被过滤到上面什么都没有,你甚至看不到WebView,它可能会加载url,你就是看不到它。

Good luck.

祝你好运。

#2


3  

UPDATE: with setJavaScriptEnabled(false);, the Url "https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655" can displayed successfully in the WebView.

更新:使用setjavastenabled (false);, Url“https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655”可以在WebView中成功显示。

Android WebView不加载URL?

public class WebViewActivityFragment extends Fragment {

    public WebViewActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //return inflater.inflate(R.layout.fragment_web_view, container, false);

        View v = inflater.inflate(R.layout.fragment_web_view, container, false);

        final String url = "https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655";//"https://google.com.vn";
        final ProgressBar progressBar = (ProgressBar)v.findViewById(R.id.progressBar);
        progressBar.setMax(100);
        final TextView titleView = (TextView)v.findViewById(R.id.titleView);
        WebView mWebView = (WebView) v.findViewById(R.id.webView);
        mWebView.setInitialScale(1);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        mWebView.getSettings().setUseWideViewPort(true);
        mWebView.getSettings().setJavaScriptEnabled(false);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAllowContentAccess(true);
        mWebView.setScrollbarFadingEnabled(false);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return false;
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                if (progress == 100) {
                    progressBar.setVisibility(View.INVISIBLE);
                    progressBar.setProgress(0);
                } else {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(progress);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                titleView.setText(title);
            }


        });
        mWebView.loadUrl(url);

        return v;
    }
}

I have used your entire code, here my web view result. I think perhaps that website is not mobile-ready because I tried to open your 2 urls in web browser in my phone the same problem (still loading...)

我已经使用了你的全部代码,这是我的web view结果。我想这个网站可能不适合移动,因为我试着在我的手机里打开你的两个网址同样的问题(仍然在加载…)

Android WebView不加载URL?

#3


3  

if you want you can try to use Indeterminate Progress Bar Example and remove setWebChromeClient try this code :

如果你想要,你可以尝试使用不确定的进度条示例并删除setWebChromeClient试试以下代码:

public class WebViewActivityFragment extends Fragment {

 public WebViewActivityFragment() {
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //return inflater.inflate(R.layout.fragment_web_view, container, false);

    View v = inflater.inflate(R.layout.fragment_web_view, container, false);

    final String url = "https://buchung.salonmeister.de/ort/301655/menue/#offerId=907601&venueId=301655";//"https://google.com.vn";
    final ProgressBar progressBar = (ProgressBar)v.findViewById(R.id.progressBar);
    progressBar.setMax(100);
    progressBar.setVisibility(View.GONE);
    final TextView titleView = (TextView)v.findViewById(R.id.titleView);
    WebView mWebView = (WebView) v.findViewById(R.id.webView);
    mWebView.setInitialScale(1);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.getSettings().setJavaScriptEnabled(false);
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setAllowContentAccess(true);
    mWebView.setScrollbarFadingEnabled(false);
    mWebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

         @Override
        public void onPageFinished(WebView view, String url) {
         progressBar.setVisibility(View.GONE);
         progressBar.setProgress(100);
         super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
         progressBar.setVisibility(View.VISIBLE);
         progressBar.setProgress(0);
         super.onPageStarted(view, url, favicon);
        }
    });


    mWebView.loadUrl(url);

    return v;
}

}

}

#4


2  

Your URL is https so It's ssl problem.

你的URL是https,所以这是ssl问题。

You can find a solution in other question.

你可以在其他问题中找到答案。

mWebView.setWebViewClient(new WebViewClient() {

    @Override
    public boolean shouldOverride UrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed(); // Ignore SSL certificate errors
    }
});

#5


0  

Make sure you to put all the permissions in menifest file outside of application tag.

确保您将所有权限放在应用程序标记之外的menifest文件中。

#6


0  

This may solve your issue :

这可能会解决你的问题:

webView.post(new Runnable() {
    @Override
    public void run() {
        webView.loadUrl(url);
    }
});