Android网络开发框架Retrofit(二:入门篇,hello, world)

来源:互联网 发布:关于猫的淘宝昵称 编辑:程序博客网 时间:2024/06/07 02:21

Android网络开发框架Retrofit(二:入门篇,hello, world)

今天介绍一下Retrofit的使用方法


Android Studio配置环境:

在build.gradle里面增加

    compile 'com.squareup.retrofit:retrofit:2.0.0-beta3'    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta3'

eclipse配置环境:

去Retrofit的github中下载代码包,然后新建模块,进行引用,地址为https://github.com/square/retrofit/archive/parent-2.0.0-beta2.zip


然后配置请求的URL和参数 
定义一个接口

public interface APIService {    @GET("/service/getIpInfo.php")    Call<DemoBean> getIpInfo(@Query("ip") String ip);}

在接口里面,用注解来配置要请求的URL,支持的方法有@GET、@POST、@HEAD、@PUT、@DELETA、@PATCH 

再看定义的方法,里面也有注解,括号里面的是配置参数,注解有@Path、@Query、@QueryMap、@Body、@Field、@Multipart、@Headers

配置请求的类型@GET或@POST,这些方法在这里不详解, 
URL的配置,有两种 
1、/login.php 
2、http://www.csdn.net/login.php 
建议使用第一种

解析来讲一下配置参数的注解 
@Path,用于配置一个URL中的某一部分

@GET("/users/{username}")Call<User> getUser(@Path("username") String username);
这样,就可以动态修改URL中的某部分了


@Query,用于配置请求的表单参数

@GET("/service/getIpInfo.php")Call<DemoBean> getIpInfo(@Query("ip") String ip);


@QueryMap,传入一个Map对象,这个对象里面配置了键值对

@GET("/queryList.php")Call<DemoBean> queryList(@QueryMap Map<String, String> options);

@Body,是一个实体类对象,对象里面可以设置各种值,然后会自动序列化

@POST("/users/new")Call<User> createUser(@Body User user);

说一下Retrofit的调用方法,主要两种,分别是同步和异步 

同步方法是阻塞的,所以要开启一个线程来使用

String baseUrl = "http://ip.taobao.com";Retrofit retrofit = new Retrofit.Builder()                      .baseUrl(baseUrl)  .addConverterFactory(GsonConverterFactory.create())  .build();APIService apiService = retrofit.create(APIService.class);Call<DemoBean> call = apiService.getIpInfo(ip);try {Response<DemoBean> response = call.execute();DemoBean demoBean = response.body(); } catch (IOException e) {e.printStackTrace();}
异步的:

String baseUrl = "http://ip.taobao.com";        Retrofit retrofit = new Retrofit.Builder()                .baseUrl(baseUrl)                .addConverterFactory(GsonConverterFactory.create())                .build();        APIService apiService = retrofit.create(APIService.class);        Call<DemoBean> call = apiService.getIpInfo(ip);        call.enqueue(new Callback<DemoBean>() {            @Override            public void onResponse(Response<DemoBean> response, Retrofit retrofit) {                DemoBean demoBean = response.body();            }            @Override            public void onFailure(Throwable t) {            }        });

看代码,使用之前是要创建一个Retrofit对象,这个对象又使用create方法创建出接口对象,这里用到的是Java的动态代理,之所以这样做,Retrofit是要通过动态代理,将接口里定义的方法,和传进的值,获取下来,进行数据的请求,在这里先不详细说这个动态代理,有兴趣的童鞋另行了解

.addConverterFactory(GsonConverterFactory.create())
这个是添加一个用Gson解析数据的配置,Retrofit支持多种解析方式

Gson: com.squareup.retrofit2:converter-gsonJackson: com.squareup.retrofit2:converter-jacksonMoshi: com.squareup.retrofit2:converter-moshiProtobuf: com.squareup.retrofit2:converter-protobufWire: com.squareup.retrofit2:converter-wireSimple XML: com.squareup.retrofit2:converter-simplexmlScalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
挑自己喜欢的去配置吧


本节教程结束!



教程例子源码下载



3 0
原创粉丝点击