Disqus Login and Logout in Android

Disqus login and logout features as described in the previous blog entry on Disqus (http://globeotter.com/blog/disqus-android-code/) do not work as intended in the latest updates. Currently if you followed the Code the login will stay busy and the page has to be refreshed. You can see the busy signal in the screenshot below. To get rid of it you have to change Activities and come back to the Disqus page (in which you are auto logged in).  After looking into it a bit, we found a nice work around for these issues.

Disqus login and logout created a busy signal in this screenshot

If you would like to see how the code works you can look at our new App we made to Demo the functionality, The Daily Forum https://play.google.com/store/apps/details?id=otter.forum

The solution to the Disqus login and logout issues are contained in the WebViewClient in Android. It is not picking up the redirect and refreshing the page in the javascript. To address this we will need a new class that extends WebViewClient

MyWebViewClient.java

public class MyWebViewClient extends WebViewClient {

private String myUrl;
 public MyWebViewClient(String _myURL) {
 // TODO Auto-generated constructor stub
 myUrl=_myURL;

 }
 @Override
 public void onPageStarted(WebView view, String url, Bitmap favicon) {
 Log.i("page started", url);
 }

@Override
 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
 Log.i("disqus error", "failed: " + failingUrl + ", error code: " + errorCode + " [" + description + "]");
 }

public void onPageFinished(WebView view, String url) {
 if(url.indexOf("logout")>-1 || url.indexOf("disqus.com/next/login-success")>-1 ){
 view.loadUrl(myUrl);

 }
 if(url.indexOf("disqus.com/_ax/twitter/complete")>-1||url.indexOf("disqus.com/_ax/facebook/complete")>-1||url.indexOf("disqus.com/_ax/google/complete")>-1){
 view.loadUrl("YOUR_URL/login.php");

 }
 if(url.indexOf("YOUR_URL/login.php")>-1){
 view.loadUrl(myUrl);
}
}
}

There are a few key features of this code. First it is listening for the Twitter,Facebook, and Google Plus login code. Your WebViewClient will now be looking for this URL “disqus.com/_ax”. When it detects this we send it to a blank URL login.php. This login page does nothing, we only use it so we can detect that the 3rd party login was successful. Second, after we have either detected a 3rd party login (via login.php) or have been logged in via disqus (“disqus.com/next/login-success”) we refresh our URL (this is the URL passed in that your disqus page was using). Third, the logout code is quite simple. When a logout is detected simply refresh the url as well.

Next we need to actually call this class. We will use the same Disqus.java class as the previous blog entry on Disqus, but this time we will call MyWebViewClient

Disqus.java

</pre>
public abstract class Disqus extends Activity
{
private WebView webDisqus;

protected void onCreate(Bundle savedInstanceState)
{

//set layout to disqus xml
String url="http:///showcomments.php?disqus_id="+insert disqus thread is here;
setContentView (R.layout.disqus);

webDisqus = (WebView) findViewById(R.id.disqus);
 //set up disqus
 WebSettings webSettings2 = webDisqus.getSettings();

webSettings2.setJavaScriptEnabled(true);

webSettings2.setBuiltInZoomControls(true);

webDisqus.requestFocusFromTouch();

//set to the new WebViewClient Class
webDisqus.setWebViewClient(new MyWebViewClient(url));

webDisqus.setWebChromeClient(new WebChromeClient());

webDisqus.loadUrl(url);

}

}

We hope this helps if you were struggling with the Disqus login and logout in Android. Let us know if it works for you. If you get a chance check out the Daily Forum and leave us a review!

  • Pingback: DISQUS Android Code Walkthrough - GlobeOtter()

  • nasznjoka

    HI! I think there’s a bug there on

    //set to the new WebViewClient Class
    webDisqus.setWebViewClient(new MyWebViewClient());
    Bacause the class which is called have a constructor
    public MyWebViewClient(String _myURL) {
    // TODO Auto-generated constructor stub
    myUrl=_myURL;

    }
    and you didn’t show it there. I think its a typo, now i copied the two classes together but it fails to run mind if you can help with that? Thanks

    • Hi,

      The code is changed. You are right MyWebViewClient requires a URL String. So I would set that ahead of time and pass it into the WebViewClient and the loadURL method.

      Let me know if this works for you. You should see the change in line 10 and 24.

      • nasznjoka

        But i’ve downloaded your app its a pretty nice idea i had it once. I know it has worked that’s why it works like charm (the app).Congrats I’ve searched and seems no one has figured out how to listen to the webview url in android not even in stackoverflow. Let me implement it

        • Thanks 🙂 It took a little while but it was a fun challenge. We made the app as a demo but hopefully people will download it and use it. A Forum is pretty boring without users but it at least shows the technology.

          Let us know if you need any help with the implementation.

          • nasznjoka

            oops it still gives the same error Caused by: java.lang.InstantiationException: and why don’t you make it support pre honey comb devices?

          • Hmm not an error we have seen so I’m not sure what is going on without seeing the code (if you want you can send some snippits of the code to support@globeotter.com and I’ll take a look).

            WebView changed in the more recent Android versions to have better javascript support so I made it for those versions. I think we could get it to work for earlier versions but that is a decreasing amount of phones.

          • nasznjoka

            ok am sending right a way cause my app only awaits for this intergration to go live. Thanks

          • nasznjoka

            i’ve already sent the snippet

          • nasznjoka

            OOps am so dumb that resource id is used on another thing in the app so bacause i copied your code as it is it accepted while its not the id of my layout. Plus onCreate is not called through super.oncreate there above also was throwing the error because of it. Now it works and it refreshes as well thanks Bro

  • nasznjoka
  • nasznjoka

    Another issue has emerged, implementing progessdialog in this way of webiveiew implementation is a pain in the ### cause there’s no OnShouldOverrideLoading hence its hard to implement how i know

    • Yes I have not tried anything with Disqus in a ProgressDialog. I think maybe we could extend Dialog and make our own. If I get time later this month I will try it out and see what I find.

  • Sweety

    Can I get the disqus comments data instead of loading the web view?? So that I can create the way I want to display the comments in my app

    • You would need to go through the Disqus API for that https://disqus.com/api/docs/

      You can capture somethings from the page and store them in your own database without using the disqus API but it would probably be easier to go through that API.

      • Sweety

        Is it possible to control the login from the app and send to the web view? In the list of options to login, display the option to login from app. When clicked redirects to an Actiivity for login and then launch web view?

        • So you will be stuck with the facebook/twitter/disquss logins from this methodology.

          I believe you can add it using Single Sign on see this link http://help.disqus.com/customer/portal/articles/684744-getting-started-with-single-sign-on

          I unfortunately do not have a tutorial on using this though. I will tyr to add it in the future.

          • Sweety

            I am trying to use the login feature the website does. But once I login it says the page doesn’t exist. I am not using the above methodology but using the one used by website. Something like this: this.sso = {

            name: “Philly.com”,

            button: “http://media.philly.com/designimages/partnerIcon-Phillycom.jpg”,

            icon: “http://media.philly.com/designimages/favicon.png”,

            url: “http://www.philly.com/s?action=login&popup=y&disqus=sso&requested=y&rurl=/u/?dir=/philly/userarea/confirmation/You_are_now_signed_in.html%3Fpopup%3Dy%26disqus%3Dsso%26tracking%3Dnone&signupRURL=http%3A%2F%2Fwww.philly.com%2Fphilly%2Fhp%2Fnews_update%2F253695161.html”,

            logout: “http://www.philly.com/s?action=doLogout&rurl=/s?action=login”,

            width: “600”,

            height: “350”

            can I fix this by using your login logout code, and replace “”YOUR_URL/login.php”)” with the url in the script I mentioned above?

  • Disquspotential

    Hey there!
    The Disqus comment box in votter looks pretty amazing:
    https://www.dropbox.com/s/7g4vjah3a0ybww7/Screenshot_2014-05-13-20-53-30.png

    Whereas mine:
    https://www.dropbox.com/s/k5fjgbq1jes8s5p/Screenshot_2014-05-13-20-59-43.png
    …Is simply ugly.
    I tried squeezing in some CSS and it only made it worse. Plus I keep getting this weird logcat error:

    https://www.dropbox.com/s/mehicxclomimsx4/2014513205358.jpg

    Could you possibly walk me through as to how to make it more normal, like the one in votter? Did you do something extra? P.S: I need to use the ‘for darker backgrounds’ since my website has a dark theme. Without tweaking the CSS it gave white text on white background. The greyish background above is done by me. I tried giving it a margin, which squished the whole thing. So, how could I make mine more like yours?
    Thank you! 🙂

  • KPOP TOP10

    Thank you 😉

    I fixed ..

    if(url.indexOf(“logout”)>-1 || url.indexOf(“disqus.com/next/login-success”)>-1 ){
    //view.loadUrl(myUrl);
    view.loadUrl(“YOUR_URL/login.php”);
    }

  • softdrinker

    Really nice your cheat code !!! +1

    Edit: But there is a little problem, when trying use back button to return, we have to click a lot of time the back button 🙂 But it’s not really a big problem for now ^^

    • What are you returning to?

      • softdrinker

        Before to go to disqus fragment webview, I have another simple fragment.
        And the problem just become when we log in to disqus (I think it’s caused by the redirect), so I’ve to press a lot of time on the back button to reach the initial simple fragment

        • Just a guess but you could watch for the backpress by overriding
          @Override
          public void onBackPressed() {
          //skip back to you simple fragment by popping backstack
          }

      • Nos #WhereIsNabil?

        hi, i have implemented part of your code, i access to a website (containing a disqus forum) via a webview. i have to login in disquS, i put login password, i get back the login success message “https://disqus.com/next/login-…”, the 3 busy signals disappears and i go back to my page with the disquS forum but unfortunately i’m not logged in, i still have to identify… someonoe knows how this problem occurs?

        • I am experiencing exactly the same issue, after loggin in, the comments page is reloaded but doesnt show me as logged in, it still gives me the login button.

  • DanielGrech

    Quick help for anyone targeting Android 5.0+, you’ll also need to add this line of code to get the login to work:

    CookieManager.getInstance().setAcceptThirdPartyCookies(content, true);

    Without this, the whole login flow succeeds, but the UI wont reflect a logged in state

  • Pauline 888

    Hello, I’m currently trying to set up disqus in my app but i’ve been stuck for several days now… I do not fully understand this solution… by which URL can we replace the variable YOUR_URL if we have to replace it ? and how to find disqus identifier if they are set ? I don’t even know whether I success to login or not :/

  • Pauline 888

    Hello, I’m currently trying to set up disqus in my app but i’ve been stuck for several days now… I do not fully understand this solution… by which URL can we replace the variable YOUR_URL if we have to replace it ? and how to find disqus identifier if they are set ? I don’t even know whether I success to login or not :/

  • If you would like to learn how to use the following objects to write an Android application that displays a vertically upward scrolling Rainbow of colours in a FREE video, then click the link at the end of this comment:

    . LinearLayout
    . Activity
    . View
    . Paint
    . Canvas
    . ArrayList

    http://androidprogrammeringcorner.blogspot.com/2015/06/pak-longs-android-programming-corner_24.html

  • invincible1388

    I have successfuly implemented the code and its working fine on all the devices < 5.0 version. On Android 5.0 the login works but after reload the session gets lost and says login again. Any one else has faced the similiar issue?

    • Björn Kapunkt

      Starting with 5.0 you need to allow your webview to accept third party cookies, you can do it like this:
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
      }

      • Bunty

        Thanks a lot it works for me

  • sunil

    It’s great tutorial

  • Björn Kapunkt

    Thanks for sharing! Does anyone else have the problem, that the Facebook login page does not fill the width of the view and is impossible to use? Any solutions? Thanks

  • Bunty

    After successfully log in it redirect me to the url but the UI remain same as previous asking for login again

  • Nice instructions. Recently, I have problems with disqus comment system. My site often denies comments from guest. Don’t know why this happens.

  • Vipul Behl

    While using google to sign in, as soon as i press the textbox to enter my google id, it automatically redirects me to the default page. Without logging me in. How can I resolve this issue?

  • krishna upadhya

    I am still facing busy signal issue in login page after login through disqus, I am not getting control back after success login , it displays blank page with busy signal. I have added the same code as above.

  • Nos #WhereIsNabil?

    hi, i have implemented part of your code, i access to a website (containing a disqus forum) via a webview. i have to login in disquS, i put login password, i get back the login success message “https://disqus.com/next/login-success/#!auth%3Asuccess”, the 3 busy signals disappears and i go back to my page with the disquS forum but unfortunately i’m not logged in, i still have to identify… someonoe knows how this problem occurs?

  • Saurabhniranjan2

    Thanks for the code. But i need help with Login and Logout. I am able to show the comments but whenever i try to login with any of the social net it takes me to a Disqus error apge showing an error- :There was an error submitting the form”. If try to login with Disqus, i see an image of three tyres rotating and thts it. Plz HELP.