Android-logon to Digu Network

Source: Internet
Author: User

After searching for a long time, I finally found an open-source Android client source code. Because I have never done this application before, I have to learn it from the beginning. Before I started this article, I had read a lot of articles about the network connection of the Android platform, but I was always confused. I think my main reason is: a pair of java.net. * You are not familiar with other interfaces. In other words, you are not familiar with the HTTP protocol. The second is that you do not know how to use the network APIs. After reading this article on Android based on the HTTP protocol, I probably learned some basic information about the HTTP protocol, and then I went to check the Kirin source code, to truly understand how to implement the login process. So I strongly recommend that you read this article and the code.

The following code is a preliminary login verification process. After successful login, four basic information of the user will be displayed. I requested an XML data here. For details, see the API, in addition, I thought XXXX replaced the cbin password. We recommend that you register an account on your own. Here, I use the status code returned by the server to directly determine whether the logon is successful, when using the Kirin method, if the user name and password are incorrect, an exception is reported, that is, its logon is still a problem strictly, this method is based on my personal understanding that the status code in HTTP-based download of Android is 401, and I do not know whether this process is reasonable.

Code

 Private Boolean logindidu (){
String spec = "http://api.minicloud.com.cn/account/verify.xml? Isallinfo = false ";
Try {
URL url = new URL (SPEC );
Httpurlconnection connect = (httpurlconnection) URL. openconnection ();
// Set the permission to read server resources
Connect. setdooutput (true );
// Set the timeout time for connecting to the server
Connect. setconnecttimeout (10*1000 );
// Set the time-out period for reading resources from the server
Connect. setreadtimeout (30*1000 );
// SET THE REQUEST METHOD
Connect. setrequestmethod ("get ");
// Authorization request header domain is mainly used to prove that the client has the right to view a resource
// Used for user authentication (for details, see API: http://code.google.com/p/digu-api/wiki/DiguApi)
String user = "cbin: XXXX ";
Connect. setrequestproperty ("Authorization", "Basic" + base64.encodebytes (user. getbytes ()));
Int code = connect. getresponsecode ();
Log. V (TAG, "code ----------" + Code );
If (code> = 0 & Code <299 ){
Connect. Connect ();
Inputstream is = connect. getinputstream ();
Inputstreamreader ISR = new inputstreamreader (is, "UTF-8 ");
Bufferedreader BR = new bufferedreader (ISR );
Stringbuffer sb = new stringbuffer ();
String Readline = NULL;
While (Readline = Br. Readline ())! = NULL ){
SB. append (Readline). append ("\ n ");
}
Showtext = new string (sb. tostring (). getbytes ("UTF-8"), "UTF-8 ");
Return true;
} Else if (code = 401 ){
// If the status code returned by the server is 401, the authorization fails (that is, the logon fails)
Return false;
}
Log. V (TAG, "code -----------" + Code );
} Catch (malformedurlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return false;
}

 

 

 

 

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.