WebView.loadUrl(url) 不执行任何操作

发布于 2024-11-25 09:10:03 字数 2113 浏览 0 评论 0原文

我最近为 Android 编写了一个简单的 Twitter 应用程序,以了解 Twitter API 和 OAuth 的基本知识。

该应用程序的主要活动只是要求输入用户名。然后它调用另一个处理 OAuth 的活动。 Twitter API 调用。它将用户重定向到授权页面,然后在用户完成后返回到应用程序。

它曾经工作得很好,但现在由于某种原因,当我调用 webview.loadUrl(authorizationURL) 时,什么也没有发生。不过,我从未更改过任何会影响 WebView 内容的内容...这是我的代码:

@Override
public void onResume() {
    super.onResume();
    try {

        if(weNeedCredentials()) {
            obtainCredentials();
        }

        follow(mUsername);

    } catch (OAuthException oae) {
        // omitted
    }
}


private boolean weNeedCredentials() {
    // assume the method returns true
}


private void obtainCredentials() {
    final Token requestToken = mOauthService.getRequestToken();
    String authUrl = mOauthService.getAuthorizationUrl(requestToken);
    // I verified that authUrl is the correct url (and != null)

    final WebView oauthView = (WebView) findViewById(R.id.oauthview);

    oauthView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(someCondition) {
                oauthView.setVisibility(View.GONE);

                doOtherStuff();

                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }
    });

    oauthView.getSettings().setJavaScriptEnabled(true);
    oauthView.loadUrl(authUrl);

}

这是我的布局 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <WebView 
        android:id="@+id/oauthview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

</LinearLayout>

而且我还在清单中包含了正确的 Internet 权限。

WebViewCoreThread 在应用程序的生命周期内运行,WebViewWorkerThread 稍后会弹出,但不会出现任何 WebView(甚至没有白屏)。该应用程序也不会阻塞。它继续运行,就好像 loadUrl() 行被简单地注释掉一样。

我已经在我的手机(Droid X2)和模拟器上进行了测试,两者的结果相同。任何帮助将不胜感激。

提前致谢!

I recently wrote up a simple Twitter app for Android to learn the ropes of the Twitter API and OAuth.

The app's main activity simply asks for a username to follow. It then calls another activity which handles the OAuth & Twitter API calls. It redirects the user to an authorization page, which then returns to the app after the user finishes.

It used to work just fine, but now for some reason when I call webview.loadUrl(authorizationURL), NOTHING happens. I never changed anything that would affect the WebView stuff though... Here's my code:

@Override
public void onResume() {
    super.onResume();
    try {

        if(weNeedCredentials()) {
            obtainCredentials();
        }

        follow(mUsername);

    } catch (OAuthException oae) {
        // omitted
    }
}


private boolean weNeedCredentials() {
    // assume the method returns true
}


private void obtainCredentials() {
    final Token requestToken = mOauthService.getRequestToken();
    String authUrl = mOauthService.getAuthorizationUrl(requestToken);
    // I verified that authUrl is the correct url (and != null)

    final WebView oauthView = (WebView) findViewById(R.id.oauthview);

    oauthView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(someCondition) {
                oauthView.setVisibility(View.GONE);

                doOtherStuff();

                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }
    });

    oauthView.getSettings().setJavaScriptEnabled(true);
    oauthView.loadUrl(authUrl);

}

Here's my layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <WebView 
        android:id="@+id/oauthview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

</LinearLayout>

and also I included the proper Internet permissions in the Manifest.

A WebViewCoreThread is running for the life of the app, and a WebViewWorkerThread pops in later, but no WebView ever comes up (not even a white screen). The app never blocks either. It continues running as if the loadUrl() line were simply commented out.

I've tested on my phone (Droid X2) as well as an emulator, both with the same results. Any help would be greatly appreciated.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2024-12-02 09:10:03

它继续运行,就像 loadUrl() 行被简单地注释掉一样。

它将始终“继续运行,就好像 loadUrl() 行被简单地注释掉一样”。 loadUrl() 是异步的并且不会阻塞。

即兴,要么:

  • weNeedCredentials() 返回 false,或者
  • follow() 正在替换 UI,或者
  • Twitter 正在执行重定向,并且 someConditiontrue,因此您要立即使 WebView 变为 GONE
  • 因为该 URL 存在问题你正在加载

It continues running as if the loadUrl() line were simply commented out.

It will always "continue running as if the loadUrl() line were simply commented out". loadUrl() is asynchronous and does not block.

Off the cuff, either:

  • weNeedCredentials() is returning false, or
  • follow() is replacing the UI, or
  • Twitter is doing a redirect, and someCondition is true, so you are making the WebView be GONE right away
  • there are issues with the URL that you are loading
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文