Android Implementation Discuz Forum Login

Source: Internet
Author: User

Today's work encountered a problem, demand is such, need to give DZ forum to do an android extension, which must not be the member login, you need two times to develop DZ provide a login interface, vertigo ...

Because I am not familiar with DZ, so this job is still difficult for me.

After a careful analysis, why do I need a client login on Android? The answer is yes, just to get the information that uniquely identifies the user--uid, in fact, the purpose of the login is so simple (provide interface is not a UID to the client).

Since just to get a uid, we should have other ways to get, first of all think of the way to login with the Web, because DZ has provided a WAP module, it is possible to webview in the application to display a login interface to let users log in.

That's the question, how do we get his UID after the user logs in? After all, not logged in in our code ...

Repeated search, finally found a call: Cookiemanager, this class can get WebView cookies, then we directly from the cookie to obtain the UID is not OK?

With excitement, open Chrome's debug, constantly login ... Exit... is to find the UID figure, of course, the result is disappointing.


The cookie does not seem to be what we want, to find a dz big God asked, originally DZ is the encrypted UID is written to the cookie.

But how can we just give up? Soon I had an idea, since there is no uid, why don't we write the UID into a cookie? Yes, open it up! Then we need to the DZ source knife, first to find the DZ processing login files, in order to process the login code to write their own cookie value. Again to DZ big God consult, soon, this file has:/source/class/class_member.php, is this file processing of login, open look, very long, but there is a method caught my attention: function On_login (), Android should be seen at a glance, this is the login "callback", notice the callback I quoted, but the small details we do not care, the main, we took a big step, continue to browse the code, I found that I am still very lucky, soon in the position of about 275 lines, I found a section of JS code, Why does this JS code draw my attention? You'll see: ' SetTimeout ("Window.location.href =\". $href. ' \ '; ', 3000); ' We all know that the success of the login in DZ will be a few seconds to wait and then jump, find this code is very exciting, it is in this code before and after looking to see if there is no location to set our cookies.

Looking up the code, another exciting point: if (defined (' In_mobile ')) This is to differentiate between mobile phone and PC login? , apart set the UID cookie here: Dsetcookie (' uid ', $_g[' uid '); Alas? How does PHP set a cookie function differently? When you look at this source file, you will find a lot of Dsetcookie code, this must be DZ to setcookie a package, we directly copy with OK, and a $_g[' uid '] is found in this file, I guess it is the UID of the user.

Just test it, OK ... No UID is found in the cookie. Let's put this line of cookies on the Internet. The code goes up again is an if statement, this time I skip this if, directly on the above if parked Dsetcookie (' uid ', $_g[' uid '); This sentence, continue to follow up the test, this time finally in the cookie more than one, and, exactly, the item is the UID end:



Little excitement, now we can get the value of the UID in the cookie completely.

Next is the task of the Android client, it must be a webview loading DZ login interface, this will not say, to see how the cookie is handled.

In the Onpagefinished method of the Webviewclient:

Cookiemanager Cookiemanager = Cookiemanager.getinstance (); String cookie = cookiemanager.getcookie (URL); if (null! = cookie) {parseuid (cookies);}
The first line, we get the Cookiemanager instance through the static method of Cookiemanager, and then the second line through the GetCookie (String url) method, the change URL to obtain the cookie, is not very convenient?

In the IF call a Parseuid method, it is clear that we are going to fetch the UID, and then go inside to see:

private void Parseuid (String cookie) {string[] cookiekeyvalues = Cookies.split (";"); for (string cookieitem:cookiekeyvalues) {if (Cookieitem.trim (). StartsWith ("Xxx_xxxx_uid")) {string uid = Cookieitem.split ("=") [1]; System.out.println ("UID =" + uid); Sharedpreferences.editor Editor = getsharedpreferences (Constants.config, Context.mode_private). Edit (); Editor.putboolean (Constants.is_login, True); Editor.putint (Constants.uid, Integer.parseint (UID)); Editor.commit (); Finish ();}}}
In this method, we first use; separate each cookie, because I find that multiple cookies are separated, followed by a traversal, to find the key to the cookie we care about, and then continue with the = split, because the cookie in the string is key= exists in the form of value. At this point, we need to find the UID, the next few lines of code is written to the local, not much to say.


Summarize:

Sometimes, something seemingly complex can be done in a very simple way, but it requires us to change our mind and think about it from another point of thinking.

Of course, there must be an imperfect way above, and this method is not necessary for people who are familiar with DZ, but the result is OK, after all, we save a lot of code, the server only use a line of code, the client is just less than 20 lines of code to be done.

Android Implementation Discuz Forum Login

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.