Android 有没有更好的方法来刷新 WebView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2563325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:20:21  来源:igfitidea点击:

Is there a better way to refresh WebView?

androidrefreshwebviewreload

提问by cdg

Ok. I have looked EVERYWHERE and my little brain just can't understand a better way to refresh an activity. Any suggestions that I can understand would be great. :)

好的。我已经四处看了看,我的小脑袋无法理解刷新活动的更好方法。任何我能理解的建议都会很棒。:)

Here is the java code:

这是Java代码:

package com.dge.dges;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class dgeActivity extends Activity {
 WebView mWebView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings();
        mWebView.loadUrl("http://www.websitehere.php");

        Button newButton = (Button)findViewById(R.id.new_button);
        newButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    Intent intent = new Intent(dgeActivity.this, dgeActivity.class);
    startActivity(intent);
   }
        });

    }
}

And here is the main.xml

这是 main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/RelativeLayout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#000000"

>

 <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:scrollbars="none"

 />

 <Button
  android:id="@+id/new_button"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_centerHorizontal="true"
  android:text="Refresh"
 />





</RelativeLayout>

I don't like the idea of just re-stacking activity after activity. There has to be an easier way to refresh the webview. Please help. :)

我不喜欢在活动后重新堆叠活动的想法。必须有一种更简单的方法来刷新 webview。请帮忙。:)

回答by Josh Hofing

You could call an mWebView.reload();That's what it does

你可以打电话给mWebView.reload();这就是它的作用

回答by Pentium10

1) In case you want to reload the same URL:

1) 如果您想重新加载相同的 URL:

mWebView.loadUrl("http://www.websitehere.php");

so the full code would be

所以完整的代码将是

newButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    dgeActivity.this.mWebView.loadUrl("http://www.websitehere.php");
   }});

2) You can also call mWebView.reload()but be aware this reposts a page if the request was POST, so only works correctly with GET.

2)您也可以调用,mWebView.reload()但请注意,如果请求是 POST,则会重新发布页面,因此只能与 GET 一起正常工作。

回答by dragonroot

The best solution is to just do webView.loadUrl( "javascript:window.location.reload( true )" );. This should work on all versions and doesn't introduce new history entries.

最好的解决办法就是做webView.loadUrl( "javascript:window.location.reload( true )" );。这应该适用于所有版本,并且不会引入新的历史条目。

回答by Thor1401

This worked for me.

这对我有用。

just put under onCreate metode

只需放在 onCreate 方法下

Button button = (Button) findViewById(R.id.reload_btn);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            web.loadUrl( "javascript:window.location.reload( true )" );             
        }
    });

This is the code that can reload a website in a webView

这是可以在 webView 中重新加载网站的代码

web.loadUrl( "javascript:window.location.reload( true )" );  

回答by Oubaida AlQuraan

try this :

尝试这个 :

mWebView.loadUrl(mWebView.getUrl().toString());

回答by trante

Refreshing current webview's URL is not a common usage.
I used this in such a scenario: When user goes to another activity and user come back to webview's activity I reload current URL like this:

刷新当前 webview 的 URL 不是一个常见的用法。
我在这样的场景中使用了它:当用户转到另一个活动并且用户回到 webview 的活动时,我重新加载当前的 URL,如下所示:

public class MyWebviewActivity extends Activity {
    WebView mWebView;
    ....
    ....
    ....
    @Override
    public void onRestart() {
            String url = mWebView.getUrl();
            String postData = MyUtility.getOptionsDataForPOSTURL(mContext);
            mWebView.postUrl(url, EncodingUtils.getBytes(postData, "BASE64"));
    }
}

You can also use WebView's reload()function. But note that if you loaded the webview with postUrl(), then mWebView.reload();doesn't work. This also works

您也可以使用 WebView 的reload()功能。但请注意,如果您使用 加载了 webview postUrl(),则mWebView.reload();不起作用。这也有效

String webUrl = webView.getUrl();
mWebView.loadUrl(webUrl);

回答by Invincible_cooler

Override onFormResubmissionin WebViewClient

覆盖onFormResubmissionWebViewClient

@Override
public void onFormResubmission(WebView view, Message dontResend, Message resend){
   resend.sendToTarget();
}

回答by D.A.H

Why not to try this?

为什么不试试这个?

Swift code to call inside class:

在类内部调用的 Swift 代码:

self.mainFrame.reload()

or external call

或外线电话

myWebV.mainFrame.reload()

回答by mAsT3RpEE

Yes for some reason WebView.reload() causes a crash if it failed to load before (something to do with the way it handles history). This is the code I use to refresh my webview. I store the current url in self.url

是的,如果之前无法加载 WebView.reload() 会导致崩溃(与它处理历史的方式有关)。这是我用来刷新我的 webview 的代码。我将当前网址存储在self.url

# 1: Pause timeout and page loading

self.timeout.pause()
sleep(1)

# 2: Check for internet connection (Really lazy way)

while self.page().networkAccessManager().networkAccessible() == QNetworkAccessManager.NotAccessible: sleep(2)

# 3:Try again

if self.url == self.page().mainFrame().url():
    self.page().action(QWebPage.Reload)
    self.timeout.resume(60)

else:
    self.page().action(QWebPage.Stop)
    self.page().mainFrame().load(self.url)
    self.timeout.resume(30)

return False