Android 数据处理之Webapi OAuth2.0


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

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

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

我们采用gson解析JSON数据,默认情况下解析必须名称大小写一致,高级应用可以参考Gson 解析教程

首先我们定义一个实体类

/**
 * 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());
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM