Android的PhoneGap的 - 尝试设置WebViewClient当超时错误错误、Android、PhoneGap、WebViewClient

2023-09-04 10:34:45 作者:ζ商务男一枚,

我正在与Android和PhoneGap的,而此刻我有一个简单的事情烦恼。我需要设置一个webViewClient到PhoneGap的web视图,以便捕获完成了页的URL,并与工作

这是在code:

 公共类PhoneGapTest扩展DroidGap {

    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.setBooleanProperty(loadInWebView,真正的);
        super.clearCache();
        super.keepRunning = FALSE;
        super.loadUrl(文件:///android_asset/www/index.html);

        super.appView.setWebViewClient(新WebViewClient(){

          @覆盖
          公共无效onPageStarted(web视图查看,字符串URL,位图位图){

            Log.i(TEST,onPageStarted:+网址);

          }

          @覆盖
          公共无效onPageFinished(web视图查看,字符串URL){

            Log.i(TEST,onPageFinished:+网址);

          }

        });

    }
 

这code似乎并不工作,页面加载从来没有和我得到一个超时错误,但如果我删除了setWebViewClient的部分页面加载完美。

我看到有一类CordovaWebViewClient,我必须用这个来代替WebViewClient?我发现这种方式在网络上:

  this.appView.setWebViewClient(新CordovaWebViewClient(本){

          @覆盖
          公共布尔shouldOverrideUrlLoading(最终的WebView视图,字符串URL){
            Log.i(BugTest,shouldOverrideUrlLoading:+网址);
            返回true;
          }

          @覆盖
          公共无效onPageStarted(web视图查看,字符串URL,位图位图){

            Log.i(TEST,onPageStarted:+网址);

          }

          @覆盖
          公共无效onPageFinished(web视图查看,字符串URL){

            Log.i(TEST,onPageFinished:+网址);

          }

          @覆盖
          公共无效doUpdateVisitedHistory(web视图查看,字符串URL,布尔isReload){
          }

        });
 
Android WebView设置cookie

但是,code不工作要么,我仍然有一个超时错误。 我也看到,已经有webVieClient成员,但我不知道我是否要使用它,以及如何。

我正与PhoneGap的版本1.9.0

感谢您的阅读

答西蒙:

它的工作就这样,谢谢!

 公共类MainActivity扩展DroidGap {

@覆盖
公共无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);
    super.init();
    super.appView.clearCache(真正的);
    super.appView.clearHistory();
    this.appView.setWebViewClient(新CustomCordovaWebViewClient(本));
    super.loadUrl(文件:///android_asset/www/index.html);
}

公共类CustomCordovaWebViewClient扩展CordovaWebViewClient {

 公共CustomCordovaWebViewClient(DroidGap CTX){
   超(CTX);
 }

 @覆盖
 公共无效onPageStarted(web视图查看,字符串URL,位图位图){
   super.onPageStarted(查看,网址,位图);
   Log.i(TEST,onPageStarted:+网址);
 }

 @覆盖
 公共无效onPageFinished(web视图查看,字符串URL){
   super.onPageFinished(查看,网址);
   Log.i(TEST,onPageFinished:+网址);
 }

 @覆盖
 公共无效doUpdateVisitedHistory(web视图查看,字符串URL,布尔isReload){
     super.doUpdateVisitedHistory(查看,网址,isReload);
 }

 @覆盖
 公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl){
     super.onReceivedError(查看,错误code,说明,failingUrl);
 }

}

}
 

解决方案

要完成你想做的事我会延长CordovaWebViewClient类并覆盖你想要的方法是什么,但不要忘记调用父类的方法或PhoneGap的赢得了未经CordovaWebViewClient,因为它是一类重要的吨工作

I'm working with Android and Phonegap, and at the moment I'm having trouble with one simple thing. I need to setup a webViewClient to the PhoneGap webView in order to capture the URL of a page finished and to work with that.

This is the code:

public class PhoneGapTest extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.setBooleanProperty("loadInWebView", true);
        super.clearCache();
        super.keepRunning = false; 
        super.loadUrl("file:///android_asset/www/index.html");

        super.appView.setWebViewClient(new WebViewClient(){

          @Override
          public void onPageStarted(WebView view, String url, Bitmap bitmap) {

            Log.i("TEST", "onPageStarted: " + url);

          }

          @Override
          public void onPageFinished(WebView view, String url) {

            Log.i("TEST", "onPageFinished: " + url);

          }

        });

    }

That code doesn't seems to work, the page never loads and I get a TIMEOUT ERROR, but if I remove the "setWebViewClient" part the page loads perfectly.

I saw that there is a class CordovaWebViewClient, do I have to use that instead of WebViewClient? I found this way on the web:

        this.appView.setWebViewClient(new CordovaWebViewClient(this){

          @Override
          public boolean shouldOverrideUrlLoading(final WebView view, String url) { 
            Log.i("BugTest", "shouldOverrideUrlLoading: " + url); 
            return true; 
          }

          @Override
          public void onPageStarted(WebView view, String url, Bitmap bitmap) {

            Log.i("TEST", "onPageStarted: " + url);

          }

          @Override
          public void onPageFinished(WebView view, String url) {

            Log.i("TEST", "onPageFinished: " + url);

          }

          @Override
          public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){        
          }

        });

But that code isn't working either, I still got a TIMEOUT ERROR. I also saw that there is already a webVieClient member, but I don't if I have to use it and how.

I'm working with Phonegap version 1.9.0

Thanks for reading

Answer to Simon:

It worked this way, thanks!

public class MainActivity extends DroidGap {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    super.init();
    super.appView.clearCache(true);
    super.appView.clearHistory();
    this.appView.setWebViewClient(new CustomCordovaWebViewClient(this));
    super.loadUrl("file:///android_asset/www/index.html");
}

public class CustomCordovaWebViewClient extends CordovaWebViewClient {

 public CustomCordovaWebViewClient(DroidGap ctx) {
   super(ctx);
 }

 @Override
 public void onPageStarted(WebView view, String url, Bitmap bitmap) {
   super.onPageStarted(view, url, bitmap);
   Log.i("TEST", "onPageStarted: " + url);
 }

 @Override
 public void onPageFinished(WebView view, String url) {
   super.onPageFinished(view, url);
   Log.i("TEST", "onPageFinished: " + url);
 }

 @Override
 public void doUpdateVisitedHistory(WebView view, String url, boolean isReload){  
     super.doUpdateVisitedHistory(view, url, isReload);  
 }

 @Override
 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
     super.onReceivedError(view, errorCode, description, failingUrl);
 }

}

}

解决方案

To accomplish what you want to do I would extend the CordovaWebViewClient class and override the methods you want but don't forget to call the super methods or PhoneGap won't work without the CordovaWebViewClient as it is an important class.