Веб-просмотр не работает должным образом для конкретной страницы - PullRequest
1 голос
/ 03 мая 2020

Я пытаюсь реализовать веб-просмотр в своем приложении, но у меня возникли две проблемы: - Индикатор выполнения (загрузка не отображается) - Веб-просмотр не работает для этой спецификации c url (https://www.bastibazar.com/) для другого URL он работает нормально, этот URL (https://www.bastibazar.com/) работает нормально, когда я открываю его в chrome или другом браузере. Вот мой MainActivity. java

import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

 public class MainActivity extends AppCompatActivity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new myWebClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("https://www.bastibazar.com/");
    getSupportActionBar().hide();
    webView.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
            try {
                webView.stopLoading();
            } catch (Exception e) {
            }

            if (webView.canGoBack()) {
                webView.goBack();
            }

            webView.loadUrl("about:blank");
            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
            alertDialog.setTitle("No Internet Connection");
            alertDialog.setMessage("Check your internet connection and try again.");
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new 
            DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                    startActivity(getIntent());
                }
            });

            alertDialog.show();
            super.onReceivedError(webView, errorCode, description, failingUrl);
          }
        });
         }

      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) {

        view.loadUrl(url);
        return true;

    }
}

@Override
// This method is used to detect back button
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        // Let the system handle the back button
        super.onBackPressed();
    }
  }
}

Activity_main. xml

<?xml version="1.0" encoding="utf-8"?>
<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"
>



    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />



 </RelativeLayout>

И в манифесте. xml Я добавил

 <uses-permission android:name="android.permission.INTERNET" />

Может кто-нибудь предложить мне, какие изменения я должен сделать, чтобы обе функции для индикатора выполнения и веб-шоу показывает в моем приложении, спасибо заранее!

Ответы [ 2 ]

0 голосов
/ 03 мая 2020

поначалу это не работало и для меня

прочитайте это

Webview не загружает определенный веб-сайт

webView.getSettings ( ) .setDomStorageEnabled (истина);

0 голосов
/ 03 мая 2020

XML:

<?xml version="1.0" encoding="utf-8"?>
<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"
   tools:context=".MainActivity">
   <ProgressBar
      android:id="@+id/progressBar"
      android:max="3"
      android:progress="100"
      style="?android:attr/progressBarStyleLarge"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerInParent="true" />
   <WebView
      android:id="@+id/webView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_below="@+id/progressBar"
      android:layout_centerHorizontal="true" />
</RelativeLayout>

JAVA:

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
   WebView webview;
   ProgressBar progressBar;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      webview = findViewById(R.id.webView);
      progressBar = findViewById(R.id.progressBar);
      webview.setWebViewClient(new WebViewClient());
      webview.loadUrl("https://www.google.com");
   }
   public class WebViewClient extends android.webkit.WebViewClient {
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
         super.onPageStarted(view, url, favicon);
      }
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
      }
      @Override
      public void onPageFinished(WebView view, String url) {
         super.onPageFinished(view, url);
         progressBar.setVisibility(View.GONE);
      }
   }
}

androidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="app.com.sample">
   <uses-permission android:name="android.permission.INTERNET"/>
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Попробуйте, может быть, это поможет вам

Изменить URL самостоятельно

Спасибо! Удачного кодирования!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...