Retrofit2应用

一、Retrofit简介

        Retrofit是一个Square公司开源的用于 Android 和 Java 平台的类型安全的网络请求框架。Retrofit 通过将 API 抽象成 Java 接口而让我们连接到 REST web 服务变得很轻松。如果结合Rxjava使用能够大大简化代码。因为最近开发使用到Retrofit2.0,在此不作1.9版本的使用了,感兴趣再看看差别吧!

二、Retrofit使用

        1、添加依赖

/*集成retrofit依赖*/

compile'com.squareup.retrofit2:retrofit:2.2.0'

/*与Rxjava集成时用到*/

compile'com.squareup.retrofit2:adapter-rxjava:2.1.0'

/*需要将对象与json数据转换时用到*/

compile'com.squareup.retrofit2:converter-gson:2.1.0'

        2、创建接口

        2.1、GET请求

public interface ApiService {

//请求方式

@GET(Configs.VERSION_PATH)

Call    getVersionInfos();

//Configs.VERSION_PATH除去基地址的请求网址

//(如本人地址:http://www.jianshu.com/u/2834dcfecbe0

//此时基地址是:http://www.jianshu.com/,那么u/2834dcfecbe0就是此时括号中的字符串

//将这些字符串整理成常量的好处就在于可以方便管理,见名之意!)

}

        当然,VersionInfo对应的Bean类是这个:

public class VersionInfo {

/*versionCode : 版本号

* versionName : 版本名称*/

private int versionCode;

private String versionName;

public int getVersionCode() {

return versionCode;}

public String getVersionName() {

return versionName;}}


        2.2、POST请求

@FormUrlEncoded//编解码方式

@POST(Configs.ENGLISH_SENTENCE_PATH)   //请求方式

Call getEnglishSentence(

@Field("count")intcount,//参数一

@Field("showapi_appid") String showapi_appid,//参数二

@Field("showapi_sign") String showapi_sign);//参数三

         3、构造Retrofit 两种请求构造一致

/*构造retrofit*/

Retrofit retrofit =newRetrofit.Builder().baseUrl(Configs.VERSION_BASE_URL)

.addConverterFactory(GsonConverterFactory.create())

.addCallAdapterFactory(RxJavaCallAdapterFactory.create())

.build();

        4、请求网络

        4.1、GET请求

/*实现GET网络请求*/

Call infoCall = retrofit.create(ApiService.class).getVersionInfos();

infoCall.enqueue(new Callback() {

@Override

public void onResponse(Call call,Response response) {

VersionInfo versionInfo = response.body();

Log.e(TAG,versionInfo.getVersionCode()+"-----------");

Log.e(TAG,versionInfo.getVersionName()+“-----------”);

}

@Override

public void on Failure(Call call,Throwable t) {

Log.e(TAG,t.toString()+“-------------”);

}

});

        4.2、POST请求

/*POST请求*/

Call sentenceCall = retrofit.create(ApiService.class)

.getEnglishSentence(5,Configs.APPID_STRING,Configs.APPSIGN_STRING);//多了三个参数

sentenceCall.enqueue(newCallback() {

@Override

public void onResponse(Call call,Response response) {

List data = response.body()

.getShowapi_res_body()

.getData();//获取需要的数据

for(EnglishSentence.ShowapiResBodyBean.DataBean dataBean : data) {

Log.e(TAG,"--------------"+dataBean.getEnglish());

Log.e(TAG,"--------------"+dataBean.getChinese());

}

}

@Override

public void onFailure(Call call,Throwable t) {

Log.e(TAG,"---------------"+t.toString());

}

});

        5、结果

        5.1、GET请求

03-20 22:23:10.091 20787-20787/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: 1-----------

03-20 22:23:10.091 20787-20787/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: 1.0-----------

        5.2、POST请求

03-20 23:02:05.948 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------The tears blurred my line of sight, forget sadness, it's not that simple.

03-20 23:02:05.948 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------泪水模糊了我的视线,遗忘悲伤没那么简单。

03-20 23:02:05.948 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------There is a kind of courage is in full bloom, there is a beautiful name is litter.  Bloom, the more intense, the more gorgeous curtain call.

03-20 23:02:05.948 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------有一种勇气叫盛开,有一种美丽叫凋落。绽放得越强烈,谢幕得越绚烂。

03-20 23:02:05.948 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------Everybody needs some time on their own.

03-20 23:02:05.948 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------每个人都需要些独处的时间。

03-20 23:02:05.948 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------One of the most amazing thing in the world is having someone who falls in love with you thought you never had a chance with.

03-20 23:02:05.948 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------世界上最令人激动的一件事情是,你原本以为没有机会靠近的人,竟然爱上了你。

03-20 23:02:05.949 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------Your name is such ordinary but it affects my mood all round.

03-20 23:02:05.949 32163-32163/com.lizihanglove.rxjavaretrofit E/com.lizihanglove.rxjavaretrofit.MainActivity@3fa460cf: --------------你那么平凡的名字却影响我那么多的情绪。

        好啦!Retrofit的简单使用就写到这里,希望有时间能再多写写!

你可能感兴趣的:(Retrofit2应用)