Android- web视图onPageFinished两次打电话 [英] Android- Webview onPageFinished Called Twice

查看:1250
本文介绍了Android- web视图onPageFinished两次打电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过拦截重定向URL,一旦它出现在web视图确实OAuth认证的活动。然而,onPageFinished函数以某种方式调用两次出于某种原因,这确实打乱了我的申请。这里的code:

I have an activity that does OAuth authentication by intercepting the redirect url once it show up in the webview. However, the onPageFinished function is somehow called twice for some reason, which really messes up my application. Here's the code:

public class WebViewActivity extends Activity {
private WebView gWebView;
final String REDIRECT_URI = "https://localhost:5000/receive_code";
final String CLIENT_ID = "can't post it here";
final String CLIENT_SECRET = "can't post it here";
final String SCOPE = "basic names genomes analyses";
Hashtable<String, String> riskPairs;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    gWebView = (WebView) findViewById(R.id.webView1);

    gWebView.loadUrl("https://api.23andme.com/authorize/?redirect_uri="
            + REDIRECT_URI + "&response_type=code&client_id=" + CLIENT_ID
            + "&scope=" + SCOPE);

    Log.d("WEBVIEW", "got to webpage");

    gWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (url.startsWith(REDIRECT_URI)) {
                Log.d("WEBVIEW", "onpagefinished is called");
                System.out.println("got to override");
                if (url.indexOf("code=") != -1) {
                    //if the query contains code
                    String queryString = null;
                    try {
                        queryString = new URL(url).getQuery();
                    } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(queryString);
                    String[] params = queryString.split("&");
                    String code = null;
                    for (String param : params) {
                        if (param.startsWith("code=")) {
                            code = param.substring(param.indexOf('=') + 1);
                        }
                    }
                    gWebView.setVisibility(View.GONE);
                    new PostRequest().execute(code);
                    // don't go to redirectUri
                }
            }
        }
    });


}
class PostRequest extends AsyncTask<String,Void,String>{ code getting client data...}

P.S。 请不要将此标记为重复......我读过上的StackOverflow类似的问题,并呼吁ShouldOverrideUrlLoading不适合我的工作(这就是为什么我用onPageFinished()摆在首位)。

P.S. Please don't mark this as a duplicate...I've read a similar question on StackOverflow and calling ShouldOverrideUrlLoading does not work for me(which is why I used onPageFinished() in the first place).

推荐答案

如果该URL后OK onPageStarted方法开始onPageFinished,但如果输入的网址后重定向 onPageStarted开始shouldOverrideUrlLoading然后onPageFinished。 你应该只检查是否加载URL重定向或不

If the url is OK after onPageStarted method starts onPageFinished, but if the url is redirected after onPageStarted starts shouldOverrideUrlLoading and then onPageFinished. You should just check if the loading URL is redirected or not

private boolean isRedirected;

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

  if (!isRedirected) {      
    //Do something you want when starts loading
  }

  isRedirected = false;
}

如果被重定向回调URL启动此功能

If the URL is redirected the callback starts this function

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

  view.loadUrl(url);
  isRedirected = true;
  return true;
}

在做的事情在onPageFinished检查,如果回调已经进入shouldOverrideUrlLoading方法

Before doing something in onPageFinished check if the callback has entered into shouldOverrideUrlLoading method

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

  if (!isRedirected) {
    //Do something you want when finished loading 
  }
}

这篇关于Android- web视图onPageFinished两次打电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆