日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Retrofit的简单使用

發布時間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Retrofit的简单使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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>>() {@Overridepublic void onCompleted() {}@Overridepublic void onError(Throwable e) {}@Overridepublic 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() {@Overridepublic 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();

總結

以上是生活随笔為你收集整理的Retrofit的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。