Android webview page d'erreur personnalisée
Je suis en création d'applications qui utilisent WebView pour accéder à un site web en ligne. Je suis coincé où j'ai ajouter du code pour vérifier la disponibilité de page.
public class SpartanWeb extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Adds Progrss bar Support
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main);
//Makes Progress bar Visible
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
//Get Web view
mWebView = (WebView) findViewById(R.id.webView1);
WebSettings websettings = mWebView.getSettings();
websettings.setJavaScriptEnabled(true);
mWebView.stopLoading();
mWebView.clearCache(true);
mWebView.loadUrl("http://google.com");
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
}
});
//onProgressChanged
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
//bar disappear after URL is loaded, and changes string to
//Loading...
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100); //Make the bar
//disappear after URL
//is loaded
//Return the app name after finish loading
if (progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}//EOM oc
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Je suis en train d'ajouter onReceivedError mais pour une raison quelconque personnalisé la page ne se charge pas.
/** Called when the activity is first created. */
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
{
mWebView.loadUrl("file:///android_asset/error.html");
}
Veuillez indiquer quoi faire.
mettre en œuvre votre onRecievedError dans setWEbChromeClient
si j'ai mis onReceiveError comme ce
J'ai ajouté à setWebViewClient et de son travail 🙂
si j'ai mis onReceiveError comme ce
mWebView.setWebChromeClient(new WebChromeClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { mWebView.loadUrl("file:///android_asset/error.html"); }
encore la réception d'erreur par défaut - site web non disponibleJ'ai ajouté à setWebViewClient et de son travail 🙂
mWebView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { mWebView.loadUrl("file:///android_asset/greska.html"); } });
OriginalL'auteur Ivan | 2016-03-29
Vous devez vous connecter pour publier un commentaire.
Vous pouvez appeler loadErrorPage(la vue) en fonction de la
onReceivedError
fonction.Le code suivant va charger le contenu des erreurs vous avez besoin de montrer.Ici, je suis charger le fichier html avec
loadDataWithBaseURL
.ajoutez ce code ---->mWebView.setWebViewClient(nouveau WebViewClient() { @Override public void onReceivedError(WebView vue, int errorCode, String description, Chaîne failingUrl) { Log.j'("WEB_VIEW_TEST", "code d'erreur:" + errorCode); loadErrorPage(vue); } }); ======== Cela peut vous aider.
merci pour l'aide, j'ai ajouté mon code pour mWebView.setWebViewClient(nouveau WebViewClient() et il fonctionne maintenant
OriginalL'auteur
J'ai ajouté
onReceivedError
àmWebView.setWebViewClient(new WebViewClient
alors maintenant, c'est de travailler. Merci pour les conseils.OriginalL'auteur Ivan
Vous pouvez utiliser le code suivant ..
OriginalL'auteur shobhan