您的位置:首页 > 其它

Retrofit的简单使用

2016-08-25 20:30 330 查看
Retrofit是REST安卓客户端请求库。使用Retrofit可以进行GET,POST,PUT,DELETE等请求方式。
官方文档:http://square.github.io/retrofit/

1.在AndroidManifest.xml中请求了网络权限:

<uses-permission android:name="android.permission.INTERNET"/>

2.添加Gradle依赖项:

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'

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

compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta3'

compile 'io.reactivex:rxandroid:1.0.1'

Retrofit 支持许多种解析方式来解析响应数据:

Gson com.squareup.retrofit:converter-gson:2.0.0-beta2

Jackson com.squareup.retrofit:converter-jackson:2.0.0-beta1

Moshi com.squareup.retrofit:converter-moshi:2.0.0-beta1

Protobuf com.squareup.retrofit:converter-protobuf:2.0.0-beta1

Wire com.squareup.retrofit:converter-wire:2.0.0-beta1

Simple XML com.squareup.retrofit:converter-simplexml:2.0.0-beta1

3.创建API接口:

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

4.创建retrofit实例,并添加gson转换器:
public static final String API_URL = "https://api.github.com";
// Create a very simple REST adapter which points the GitHub API.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

5.创建一个Java类Contributor,用来保存接收到的数据:
public static class Contributor {
public final String login;
public final int contributions;

public Contributor(String login, int contributions) {
this.login = login;
this.contributions = contributions;
}
}

6.调用API接口:
// Create an instance of our GitHub API interface.
GitHub github = retrofit.create(GitHub.class);

// Create a call instance for looking up Retrofit contributors.
//完整的请求地址:https://api.github.com/repos/square/retrofit/contributors
Call<List<Contributor>> call = github.contributors("square", "retrofit");

// Fetch and print a list of the contributors to the library.
List<Contributor> contributors = call.execute().body();
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}

7.取消请求

我们可以终止一个请求。终止操作是对底层的httpclient执行cancel操作。即使是正在执行的请求,也能够立即终止。

call.cancel();
Retrofit与RxJava结合

1.使用Observable创建一个API接口:

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

2.创建retrofit对象实例时,通过addCallAdapterFactory来添加对RxJava的支持:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();

3.调用API接口:
// Create an instance of our GitHub API interface.
GitHub2 github = retrofit.create(GitHub2.class);
Observable<List<Contributor>> observable = github.contributors("square", "retrofit");

observable.subscribe(new Subscriber<List<Contributor>>() {
@Override
public void onCompleted() {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onNext(List<Contributor> contributors) {
for (Contributor contributor : contributors) {
System.out.println("observable " + contributor.login + " (" + contributor.contributions + ")");
}
}
});
Retrofit使用OkHttp
OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response response = chain.proceed(chain.request());
response.header(IF_MODIFIED_SINCE, "");
return response;
}
}).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(HOST)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: