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

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-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
挑自己喜欢的去配置吧


本节教程结束!



教程例子源码下载



  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值