Android Webview Facebook Login

barisatbas picture barisatbas · Jul 20, 2011 · Viewed 12.2k times · Source

In my app, I want to make users be able to login with their Facebook account. After some searches, I am able to figure out this although I don't think this way is the best method. I used a webview in my UI, and a webviewclient to sense url switchings. As I understand, On Android, I must handle all redirections in my webviewclient (Facebook redirections, several redirections happen when user set the his/her email and password). But all I need is to parse my output xml and make a decision according to output result(redirect to my home activiy or failure etc). Here is my code, Please give your suggestions if more suitable method exists.

public class FbLoginActivity extends Activity {

    String fbLoginBaseUrl = "{my server url}/facebook/redirector.jsp?";
    private ProgressDialog progressBar; 
    WebView webView;
    int count = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.fb_login);

        fbLoginBaseUrl += "usdId=NoSession:";
        fbLoginBaseUrl += Subroutines.getInstance().getDeviceId() + "_L";

        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        progressBar = ProgressDialog.show(FbLoginActivity.this, "", "Page is loading...");
        webView.setWebViewClient(new HelloWebViewClient());

        webView.loadUrl(fbLoginBaseUrl);

    }

    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            Log.v(Subroutines.TAG, url);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            Log.i(Subroutines.TAG, "Finished loading URL: " +url);

            // if login screen loading finishes, cancel the progressdialog..
            // twice redirecting happens to this sub url..
            String subFace = "m.facebook.com/login.php";
            if(url.indexOf(subFace) != -1 && ++count == 2){
                if (progressBar.isShowing()) {
                    progressBar.cancel();
                }
            }
            // Permission redirecting..
            String loginSub = "www.facebook.com/connect/uiserver.php?method=permissions.request";
            if(url.indexOf(loginSub) != -1){
                progressBar = ProgressDialog.show(FbLoginActivity.this, "", "Logging in...");
            }
            // finally if my server makes a response..
            String sub = "{my server url}/facebook/connect.jsp";
            if(url.indexOf(sub) != -1){
                Log.v(Subroutines.TAG, "my server makes a response..");

                // xml parsing stuff..
                // use url content to parse
            }
        }
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.e(Subroutines.TAG, "Error: " + description);
        }
    }
}

Answer

IamAlexAlright picture IamAlexAlright · Jul 20, 2011

I would get the Facebook SDK and use their functions

In a nutshell you do this:

public Facebook facebook = new Facebook("appID");

Then in on create or wherever:

facebook.authorize(this,String[] YourNeededPermissions, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {}

        @Override
        public void onFacebookError(FacebookError error) {}

        @Override
        public void onError(DialogError e) {}

        @Override
        public void onCancel() {}
    });

You can see this here: http://developers.facebook.com/docs/guides/mobile/#android