Webapi OAuth2.0 of Android data processing

Source: Internet
Author: User

The previous data access and processing platform was built through. NET Webapi, which describes how to access WEBAPI data via Android.

Android's common network access is to use HttpClient and httpurlconnection, okhttp, etc., where okhttp is very efficient and supports spdy, connection pooling, gzip, and HTTP caching. By default, Okhttp automatically handles common network problems, such as two-connection, SSL handshake issues. And the Webapi we're building is mainly using the rest architecture style, square's open source product retrofit, which provides a type-safe rest client for Android applications.

Retroft based on annotations, you can turn structured data into Java entity classes.

First we define an entity class

/** * Bearertoken entity class*/ Public classBearertoken {PrivateString Access_token; PrivateString Token_type; PrivateInteger expires_in; PrivateString UserName; PrivateDate issued; PrivateDate expires;  PublicString Getaccess_token () {returnAccess_token; }     Public voidSetaccess_token (String access_token) { This. Access_token =Access_token; }     PublicString Gettoken_type () {returnToken_type; }     Public voidSettoken_type (String token_type) { This. Token_type =Token_type; }     PublicInteger getexpires_in () {returnexpires_in; }     Public voidsetexpires_in (Integer expires_in) { This. expires_in =expires_in; }     PublicString GetUserName () {returnUserName; }     Public voidsetusername (String userName) { This. UserName =UserName; }     PublicDate getissued () {returnissued; }     Public voidsetissued (Date issued) { This. issued =issued; }     PublicDate getexpires () {returnexpires; }     Public voidSetExpires (Date expires) { This. expires =expires; }}

Define a service to get tokens using the retrofit annotation method

/**/Publicinterface  authenticationservice    {// Get token     @POST    call <BearerToken> getToken (@Field ("grant_type" String Granttype, @Field ("username") string username, @Field ("  Password") String password);}

Writing unit Tests

    @Test public    void Testgettoken () throws exception{        Retrofit retrofit=new retrofit.builder ()                . BASEURL (" http://localhost:2616/")                . Addconverterfactory (Gsonconverterfactory.create ())                . Build ();        AuthenticationService service=retrofit.create (authenticationservice.class);        call<bearertoken> call=service.gettoken ("Password", "Dream Autumn @com", "123456");        Bearertoken Token=call.execute (). body ();    }

Run the test, get the data to the token,

After the certification, let's look at how to get the data through token.

The entity class that defines a user information again

/**/Publicclass  UserInfo {    // user name    Private String Name;      Public String GetName () {        return  Name;    }      Public void setName (String name) {        = name;    }    }

Define an interface for obtaining user information using retrofit annotation-based methods

/**/Publicinterface  userservice {    @GET ("api/ User")    call <UserInfo> getuserinfo (@Header ("Authorization  ") String accesstoken);}

Writing unit Tests

 public  void   Testgetuserinfo () throws Exception {Retrofit Retrofit  =new   Retrofit.builder (). BASEURL (  " http://localhost:2616/ "  Span style= "color: #000000;"        >). Addconverterfactory (Gsonconverterfactory.create ()). build (); Userloginservice service  =retrofit.create (userloginservice.        Class  );        String token  =gettoken ();        Call  <UserInfo> userinfos= Service.getuserinfo (token); UserInfo info  =userinfos.execute (). body (); 
Assert.assertnotnull (info);
     Assert.assertnotnull (Info.getname ());
}

Webapi OAuth2.0 of Android data processing

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.