您的位置:首页 > 移动开发 > Android开发

Retrofit学习教程(1)-创建一个Android客户端

2017-01-05 13:28 573 查看
写在前面:

本文是我看到Retrofit官方推荐的一个Retrofit技术文档,感觉收益匪浅,特此想把文档翻译一下,大家一起学习。

原文地址:https://futurestud.io/tutorials/retrofit-getting-started-and-android-client

这是一系列关于Retrofi的技术文档。这系列文章通过几个代码用例来测试Retrofits的功能范围和扩展。

       什么是Retrofit

官方回答是: 

A type-safe REST client for Android and Java. 
你可以通过注解来描述HTTP请求,URL参数替换,并且它支持集成默认的查询参数支持。另外,他提供多块请求上传和文档上传的功能。 

如何声明(API)请求

你可以通过在retrofit官方阅读API声明文档来理解和明白如何发出请求。你将获得所有重要的信息和清晰的代码。

准备你的Android工程 

现在让我们回归代码。如果你已经创建了你的Android工程,只需要从下一章开始就可以了。不然,你可以在你最爱的IDE上创建一个新的工程,我们偏好于用Grade来构建系统,当然你也可以用Maven. 

定义依赖:Grade或者Maven

现在让我们将Retrofit设置为你工程的依赖。选择你已经创建的工程,在你的pom.xml或者build.gradle中定义retrofit和他的依赖。当在构建你的工程时运行这些指令,工程会下载并提供这些依赖包。我们这里使用需要Okio的OKHTTP作为依赖。 


Retrofit 1.9

pom.xml

<dependency>
<groupId>com.squareup.retrofit</groupId>
<artifactId>retrofit</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.2</version>
</dependency>
build.gradle

dependencies {
// Retrofit & OkHttp
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp:2.7.2'
}



Retrofit 2

如果你使用版本2的Retrofit的话,使用以下的依赖。

pom.xml

<dependency>
<groupId>com.squareup.retrofit2</groupId><artifactId>retrofit</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>2.1.0</version></dependency>


build.gradle
dependencies {// Retrofit & OkHttp
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'}


Retrofit2使用OkHTTP作为默认的网络层,并且基于此运行的。你不需要显式地定义okhttp作为你工程的依赖,除非你有特定的版本要求。 

现在你的工程已经集成了Retrofit,让我们创建了一个Android Api/http 客户端吧。 

可维护的Android客户端

在对已存在的Retroift客户端的探索当中,example
repository of Bart Kiers脱颖而出。 实际上,它是应用Retroift进行OAuth验证的一个实例。

不过,它提供了所有关于可维护Android客户端的一切必要功能。这就是为什么我们将它作为基础,并在以后的验证功能的博客推送中对他进程扩展。 
Service
Generator 

Service
Generator是我们的API/HTTP客户端的核心。在他当前状态下,他只是定义了一个方法来创建为给定的类和接口的Rest 适配器。

下面是代码:

Retrofit
1.9

public class ServiceGenerator {
public static final String API_BASE_URL = "http://your.api-base.url";
private static RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint(API_BASE_URL)
.setClient(new OkClient(new OkHttpClient()));
public static <S> S createService(Class<S> serviceClass) {
RestAdapter adapter = builder.build();
return adapter.create(serviceClass);}}

Retrofit 2

public class ServiceGenerator {
public static final String API_BASE_URL = "http://your.api-base.url";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient.build()).build();
return retrofit.create(serviceClass);}}

ServiceGenerator类使用了RestAdapter-Builder来创建一个新的基于给定URI的API的REST客户端。例如Github的

API的基础URL是https://api.github.com/。serviceClass为Api请求定义了注解类或者接口。接下来的章节会显示

Retrofit的具体用法和如何定义一个典型的客户端。

JSON Mapping

Retrofit1.9默认使用谷歌的gson。你所做就是只需要为你的响应对象定义一个类,然后这个响应对象就会被自动

关联。当使用Retrofit2,你需要对Retrofit对象显示得添加一个converter。下面,我们已经在build.grade中为我们

Retrofit2添加了Gson支持。
compile 'com.squareup.retrofit2:converter-gson:2.1.0'

现在,你需要为你的Retrofit添加converter。在Retrofit's builder上调用.addConverterFactory(GsonConverterFactory.create())来

来将Gson集成为Retroift的默认JSON
converter。

应用Retrofit

接下来,让我们举一个例子来定义一个REST客户端来从GitHub上请求数据。首先,我们创建一个接口
并定义需要的方法。
Github
客户端
接下来的代码定义了请求一个系列contributors的GithubClient客户端和方法。
Retrofit
1.9

public interface GitHubClient {
@GET("/repos/{owner}/{repo}/contributors")
List<Contributor> contributors(
@Path("owner") String owner,
@Path("repo") String repo
);}

Retrofit 2

public interface GitHubClient {
@GET("/repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributors(
@Path("owner") String owner,
@Path("repo") String repo
);}


定义的Contributor类,他提供了和响应数据相匹配的类属性。
static class Contributor {
String login;
int contributions;}


这里有一个例子,完整成了展示了Retroift从定义接口到读取数据的全过程。Code
Example
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐