码迷,mamicode.com
首页 > 移动开发 > 详细

Android 数据处理之Webapi OAuth2.0

时间:2016-07-19 13:34:39      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

前面通过.net Webapi搭建了数据访问及处理平台,以下介绍如何通过Android来访问Webapi的数据。

Android的常用的网络访问方式是使用HttpClient和HttpURLConnection、OKHttp等,其中OKHttp非常高效,支持SPDY、连接池、GZIP和 HTTP 缓存。默认情况下,OKHttp会自动处理常见的网络问题,像二次连接、SSL的握手问题。而我们现在搭建的webapi主要使用REST的架构风格,Square提供的开源产品Retrofit,为Android平台的应用提供一个类型安全的REST客户端。

Retroft基于注解,可以把结构化的数据转化为Java实体类。

首先我们定义一个实体类

/**
 * BearerToken实体类
 */
public class BearerToken {
    private String access_token;
    private String token_type;
    private Integer expires_in;
    private String userName;
    private Date issued;
    private Date expires;

    public String getAccess_token() {
        return access_token;
    }

    public void setAccess_token(String access_token) {
        this.access_token = access_token;
    }

    public String getToken_type() {
        return token_type;
    }

    public void setToken_type(String token_type) {
        this.token_type = token_type;
    }

    public Integer getExpires_in() {
        return expires_in;
    }

    public void setExpires_in(Integer expires_in) {
        this.expires_in = expires_in;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Date getIssued() {
        return issued;
    }

    public void setIssued(Date issued) {
        this.issued = issued;
    }

    public Date getExpires() {
        return expires;
    }

    public void setExpires(Date expires) {
        this.expires = expires;
    }
}

使用Retrofit注解方式定义获取token的服务

/**
 * 认证服务
 */
public interface AuthenticationService {
    //获取Token
    @POST
    Call<BearerToken> getToken(@Field("grant_type") String grantType, @Field("username") String userName, @Field("password") String password);
}

 

编写单元测试

    @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","梦秋@com","123456");
        BearerToken token=call.execute().body();
    }

运行测试,获取到token的数据,如图:  

技术分享

 搞定认证后,下面再来看看怎么通过token获取数据。

再定义一个用户信息的实体类

/**
 * 用户信息
 */
public class UserInfo {

    //用户名称
    private String Name;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }
    
}

使用Retrofit基于注解的方式定义获取用户信息的接口

/**
 * 用户处理服务
 */
public interface UserService {
    @GET("api/User")
    Call<UserInfo> getUserInfo(@Header("Authorization") String accessToken);
}

编写单元测试

public void TestGetUserInfo() throws Exception {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://localhost:2616/")
                .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());
}

Android 数据处理之Webapi OAuth2.0

标签:

原文地址:http://www.cnblogs.com/freetalent/p/5684155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!