2

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!

bmat
  • 2,084
  • 18
  • 24

1 Answers1

2

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
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Is there any way to execute loadUrl() synchronously? The WebView has to launch prior to the call to follow() in order for the app to function properly. – bmat Jul 21 '11 at 18:02
  • 1
    @Blake M.: "Is there any way to execute loadUrl() synchronously?" -- no. "The WebView has to launch prior to the call to follow() in order for the app to function properly." -- then don't call `follow()` until the page is loaded. – CommonsWare Jul 21 '11 at 18:04
  • Well that was simple enough.. works like a charm now. Thanks so much! – bmat Jul 21 '11 at 18:44