retrofit是由square公司開發的。square在github上發布了很多優秀的Android開源項目。例如:otto(事件總線),leakcanary(排查內存泄露),android-times-square(日歷控件),dagger(依賴注入),picasso(異步加載圖片),okhttp(網絡請求),retrofit(網絡請求)等等。更多square上的開源項目我們可以去square的GitHub進行查看。這次就來介紹一下retrofit的一些基本用法。retrofit是REST安卓客戶端請求庫。使用retrofit可以進行GET,POST,PUT,DELETE等請求方式。下面就來看一下retrofit的基本用法。
由于retrofit2.0與先前版本的差別還是比較大,對于不同版本之間的差異在這里就不在進行詳細區別。下面的例子也是針對于retrofit2.0進行介紹的。retrofit2.0它依賴于OkHttp,而且這部分也不再支持替換。在這里我們也不需要顯示的導入okHttp,在retrofit中已經導入okhttp3。
<dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>mockwebserver</artifactId> <scope>test</scope></dependency>1234512345在下面的例子當中采用與GitHub一些相關api進行演示。在這里首先需要添加訪問網絡的權限。
<uses-permission android:name="android.permission.INTERNET"/>11在這里我們最好查看一下retrofit的官網添加最新依賴。
compile 'com.squareup.retrofit2:retrofit:2.0.1'11在retrofit中通過一個java接口作為http請求的api接口。
public interface GitHubApi { @GET("repos/{owner}/{repo}/contributors") Call<ResponseBody> contributorsBySimpleGetCall(@Path("owner") String owner, @Path("repo") String repo);}1234512345在這里baseUrl是在創建retrofit實力的時候定義的,我們也可以在API接口中定義完整的url。在這里建議在創建baseUrl中以”/”結尾,在API中不以”/”開頭和結尾。
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .build();123123在調用API接口請求后,獲得一個json字符串,通過Gson進行解析,獲得login以及contributions。
GitHubApi repo = retrofit.create(GitHubApi.class); Call<ResponseBody> call = repo.contributorsBySimpleGetCall(mUserName, mRepo);call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { try { Gson gson = new Gson(); ArrayList<Contributor> contributorsList = gson.fromJson(response.body().string(), new TypeToken<List<Contributor>>(){}.getType()); for (Contributor contributor : contributorsList){ Log.d("login",contributor.getLogin()); Log.d("contributions",contributor.getContributions()+""); } } catch (IOException e) { e.PRintStackTrace(); } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { }});12345678910111213141516171819202122231234567891011121314151617181920212223 這樣就完成了一個http請求,上面請求的完整地址為:https://api.github.com/repos/square/retrofit/contributors 然后我們看一下運行結果:
我們可以終止一個請求。終止操作是對底層的httpclient執行cancel操作。即使是正在執行的請求,也能夠立即終止。
call.cancel();11在上面的例子中通過獲取ResponseBody后,我們自己使用Gson來解析接收到的Json格式數據。在Retrofit中當創建一個Retrofit實例的時候可以為其添加一個Json轉換器,這樣就會自動將Json格式的響應體轉換為所需要的Java對象。那么先來看一下如何根據已有的Json格式數據如何生成Java對象。當然我們可以根據已知的數據手動創建Java對象,也可以通過工具更具Json格式為我們自動生成Java對象。
在這里介紹兩種根據Json數據自動生成Java對象的工具。
可以通過訪問jsonschema2pojo網站。先來看一下它的使用方法。 上面配置中所選注解若是使用的Gson解析,可以選擇Gson,當然沒有也是可以的。對于@Generated注解若是需要保留的話添加如下依賴,也可以直接刪除@Generated注解,沒有任何影響。
GsonFormat是AndroidStudio中的一個插件,在AndroidStudio的插件選項中直接搜索安裝這個插件即可。在這里看一下是如何使用這個插件的。
在這里我們需要為retrofit添加gson轉換器的依賴。添加過converter-gson后不用再添加gson庫。在converter-gson中已經包含gson。
compile 'com.squareup.retrofit2:converter-gson:2.0.1'11在這里先創建一個Java類Contributor,用來保存接收到的數據。
public class Contributor { private String login; private Integer contributions; public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public Integer getContributions() { return contributions; } public void setContributions(Integer contributions) { this.contributions = contributions; }}12345678910111213141516171819201234567891011121314151617181920這時候修改我們的API接口。
@GET("repos/{owner}/{repo}/contributors")Call<List<Contributor>> contributorsByAddConverterGetCall(@Path("owner") String owner, @Path("repo") String repo);1212創建retrofit實例,我們通過addConverterFactory指定一個factory來對響應反序列化,在這里converters被添加的順序將是它們被Retrofit嘗試的順序。
Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .addConverterFactory(GsonConverterFactory.create()) .build();12341234調用上面所修改的API接口。
GitHubApi repo = retrofit.create(GitHubApi.class);Call<List<Contributor>> call = repo.contributorsByAddConverterGetCall(mUserName, mRepo);call.enqueue(new Callback<List<Contributor>>() { @Override public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) { List<Contributor> contributorList = response.body(); for (Contributor contributor : contributorList){ Log.d("login", contributor.getLogin()); Log.d("contributions", contributor.getContributions() + ""); } } @Override public void onFailure(Call<List<Contributor>> call, Throwable t) { }});12345678910111213141516171234567891011121314151617 最后在來看一下運行結果。 retrofit不僅僅只支持gson,還支持其他許多json解析庫。以下版本號需要與retrofit版本號保持一致,并且以retrofit官網給出的版本號為準。
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
Jackson: compile 'com.squareup.retrofit2:converter-jackson:2.0.1'
Moshi: compile 'com.squareup.retrofit2:converter-moshi:2.0.1'
Protobuf: compile 'com.squareup.retrofit2:converter-protobuf:2.0.1'
Wire: compile 'com.squareup.retrofit2:converter-wire:2.0.1'
Simple xml: compile 'com.squareup.retrofit2:converter-simplexml:2.0.1'
Scalars (primitives, boxed, and String): compile 'com.squareup.retrofit2:converter-scalars:2.0.1'
在retrofit2.0中是沒有日志功能的。但是retrofit2.0中依賴OkHttp,所以也就能夠通過OkHttp中的interceptor來實現實際的底層的請求和響應日志。在這里我們需要修改上一個retrofit實例,為其自定自定義的OkHttpClient。代碼如下:
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(httpLoggingInterceptor) .build();Retrofit retrofit = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(okHttpClient) .baseUrl("https://api.github.com/") .addConverterFactory(GsonConverterFactory.create()) .build();12345678910111234567891011還需要添加如下依賴。
compile 'com.squareup.okhttp3:logging-interceptor:3.1.2'11 其他代碼沒有任何變化,我們來看一下運行結果。
我們可以通過@Headers來添加請求頭。
@Headers({ "Accept: application/vnd.github.v3.full+json", "User-Agent: RetrofitBean-Sample-App", "name:ljd"})@GET("repos/{owner}/{repo}/contributors")Call<List<Contributor>> contributorsAndAddHeader(@Path("owner") String owner,@Path("repo") String repo);12345671234567 運行結果。
在這里我們可以直接通過call.execute()執行一個同步請求,由于不允許在主線程中進行網絡請求操作,所以我們需要再子線程中進行執行。
new Thread(new Runnable() { @Override public void run() { try { Response<List<Contributor>> response = call.execute(); List<Contributor> contributorsList = response.body(); for (Contributor contributor : contributorsList){ Log.d("login",contributor.getLogin()); Log.d("contributions",contributor.getContributions()+""); } } catch (IOException e) { e.printStackTrace(); } }}).start();1234567891011121314151612345678910111213141516 在這里看一下運行結果。
在這里無論是同步操作還是異步操作每一個call對象實例只能被執行一次。多次執行拋出如下異常。 在這里如果我們的request和respone都是一一對應的。我們通過Clone方法創建一個一模一樣的實例,并且它的開銷也是很小的。
在前面的一些例子當中我們都是采用get請求。當然我們也可以為URL指定查詢參數。使用@Query即可。
@GET("search/repositories")Call<RetrofitBean> queryRetrofitByGetCall(@Query("q")String owner, @Query("since")String time, @Query("page")int page, @Query("per_page")int per_Page);1234512345當我們的參數過多的時候我們可以通過@QueryMap注解和map對象參數來指定每個表單項的Key,value的值。
@GET("search/repositories")Call<RetrofitBean> queryRetrofitByGetCallMap(@QueryMap Map<String,String> map);1212下面的call對象實例為上面api中所返回call對象。更具所返回的json數據所創建的實體類在這里就不在貼出代碼,下載源碼詳細查看。
call.enqueue(new Callback<RetrofitBean>() { @Override public void onResponse(Call<RetrofitBean> call, Response<RetrofitBean> response) { RetrofitBean retrofit = response.body(); List<Item> list = retrofit.getItems(); if (list == null) return; Log.d(TAG, "total:" + retrofit.getTotalCount()); Log.d(TAG, "incompleteResults:" + retrofit.getIncompleteResults()); Log.d(TAG, "----------------------"); for (Item item : list) { Log.d(TAG, "name:" + item.getName()); Log.d(TAG, "full_name:" + item.getFull_name()); Log.d(TAG, "description:" + item.getDescription()); Owner owner = item.getOwner(); Log.d(TAG, "login:" + owner.getLogin()); Log.d(TAG, "type:" + owner.getType()); } } @Override public void onFailure(Call<RetrofitBean> call, Throwable t) { }});12345678910111213141516171819202122232425261234567891011121314151617181920212223242526 上面請求中的完整連接為: https://api.github.com/search/repositories?q=retrofit&since=2016-03-29&page=1&per_page=3,運行結果如下。
在Retrofit 2.0添加了一個新的注解:@Url,它允許我們直接傳入一個請求的URL。這樣以來我們可以將上一個請求的獲得的url直接傳入進來。方便了我們的操作。
@GETCall<List<Contributor>> repoContributorsPaginate(@Url String url);1212我們可以使用@FormUrlEncoded注解來發送表單數據。使用 @Field注解和參數來指定每個表單項的Key,value為參數的值。
@FormUrlEncoded@POST("user/edit")Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);123123當我們有很多個表單參數時可以通過@FieldMap注解和Map對象參數來指定每個表單項的Key,value的值。
@FormUrlEncoded@POST("user/edit")Call<User> updateUser(@FieldMap Map<String,String> fieldMap);123123我們還可以通過@Multipart注解來發送Multipart數據。通過@Part注解來定義需要發送的文件。
@Multipart@PUT("/user/photo")User updateUser(@Part("photo") TypedFile photo, @Part("description") TypedString description);123123Retrofit能夠與RxJava進行完美結合。下面就來看一下Retrofit與RxJava是如何結合在一起的。對于RxJava在這就不在進行詳細介紹,對于RXJava的使用可以參考附錄里面給出鏈接。 首先我們需要添加如下依賴。
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'compile 'io.reactivex:rxandroid:1.1.0'1212創建retrofit對象實例時,通過addCallAdapterFactory來添加對RxJava的支持。
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(httpLoggingInterceptor) .build();Retrofit retrofit = new Retrofit.Builder() .client(okHttpClient) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .baseUrl("https://api.github.com/") .build();12345678910111234567891011使用Observable創建一個API接口。
@GET("repos/{owner}/{repo}/contributors")Observable<List<Contributor>> contributorsByRxJava(@Path("owner") String owner,@Path("repo") String repo);1212下面來調用這個API接口。
private CompositeSubscription mSubscriptions = new CompositeSubscription();11mSubscriptions.add( mGitHubService.contributorsByRxJava(mUserName, mRepo) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<List<Contributor>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(List<Contributor> contributors) { for (Contributor c : contributors) { Log.d("TAG", "login:" + c.getLogin() + " contributions:" + c.getContributions()); } } }));12345678910111213141516171819201234567891011121314151617181920 下面來看一下運行結果。 如果我們想要查看所有contributor的信息,首先我們需要向gitHub請求獲取到所有contributor,然后再通過獲得contributor進行依次向github請求獲取contributor的信息,在這時候我們使用RxJava也就非常方便了。下面看一下如何操作的。 首先再添加一個API接口。
下面在看一下是如何進行根據獲得的contributor來查看contributor的信息。
mSubscriptions.add(mGitHubService.contributorsByRxJava(mUserName, mRepo) .flatMap(new Func1<List<Contributor>, Observable<Contributor>>() { @Override public Observable<Contributor> call(List<Contributor> contributors) { return Observable.from(contributors); } }) .flatMap(new Func1<Contributor, Observable<Pair<User, Contributor>>>() { @Override public Observable<Pair<User, Contributor>> call(Contributor contributor) { Observable<User> userObservable = mGitHubService.userByRxJava(contributor.getLogin()) .filter(new Func1<User, Boolean>() { @Override public Boolean call(User user) { return !isEmpty(user.getName()) && !isEmpty(user.getEmail()); } }); return Observable.zip(userObservable, Observable.just(contributor), new Func2<User, Contributor, Pair<User, Contributor>>() { @Override public Pair<User, Contributor> call(User user, Contributor contributor) { return new Pair<>(user, contributor); } }); } }) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<Pair<User, Contributor>>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Pair<User, Contributor> pair) { User user = pair.first; Contributor contributor = pair.second; Log.d(TAG, "name:" + user.getName()); Log.d(TAG, "contributions:" + contributor.getContributions()); Log.d(TAG, "email:" + user.getEmail()); } }));123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 最后在來看下運行結果。
對Retrofit設置緩存,由于Retrofit是對OkHttp的封裝,所以我們可以直接通過OkHttpClient著手。也就是為OkHttp設置緩存。設置緩存代碼如下所示。
private OkHttpClient getCacheOkHttpClient(Context context){ final File baseDir = context.getCacheDir(); final File cacheDir = new File(baseDir, "HttpResponseCache"); Timber.e(cacheDir.getAbsolutePath()); Cache cache = new Cache(cacheDir, 10 * 1024 * 1024); //緩存可用大小為10M Interceptor REWRITE_CACHE_CONTROL_INTERCEPTOR = chain -> { Request request = chain.request(); if(!NetWorkUtils.isNetWorkAvailable(context)){ request = request.newBuilder() .cacheControl(CacheControl.FORCE_CACHE) .build(); } Response originalResponse = chain.proceed(request); if (NetWorkUtils.isNetWorkAvailable(context)) { int maxAge = 60; //在線緩存一分鐘 return originalResponse.newBuilder() .removeHeader("Pragma") .removeHeader("Cache-Control") .header("Cache-Control", "public, max-age=" + maxAge) .build(); } else { int maxStale = 60 * 60 * 24 * 4 * 7; //離線緩存4周 return originalResponse.newBuilder() .removeHeader("Pragma") .removeHeader("Cache-Control") .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale) .build(); } }; return new OkHttpClient.Builder() .addNetworkInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) .addInterceptor(REWRITE_CACHE_CONTROL_INTERCEPTOR) .cache(cache) .build();}123456789101112131415161718192021222324252627282930313233343536373839123456789101112131415161718192021222324252627282930313233343536373839在retrofit的使用中,對于文件你上傳與下載,并沒有為我們提供進度更新的接口,在這里就需要我們自己處理了。在下面的例子中給出一個文件下載的例子,并且對下載進度更新通過logcat打印出來。可以下載進行查看。到這里retrofit的基本用法也就介紹完了,對于retrofit更多的好處在使用中我們可以慢慢體會。
新聞熱點
疑難解答