Retrofit okhttp使用
来源:互联网 发布:查看应用程序端口号 编辑:程序博客网 时间:2024/06/05 15:09
前言
云水边静沐暖阳,烟波里久违的故乡,别来无恙,你在心上。
简介
今天给大家分享下现在Android中主流的网络请求库retrofit和okhttp。
我们将结合一个银行卡信息查询的案例进行介绍。
案例介绍
我们使用的是mob提供的api开放接口–银行卡信息查询点击查看,其请求示例和返回参数见下图
添加依赖
compile 'com.squareup.retrofit2:retrofit:2.3.0'compile 'com.squareup.retrofit2:converter-gson:2.3.0'compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
API封装
public class API { private static final int TIME_OUT = 8; private static API mInstance; public APIService mService; public static API getInstance() { if (mInstance == null) { OkHttpClient client = new OkHttpClient.Builder() .connectTimeout(TIME_OUT, TimeUnit.SECONDS) .build(); mInstance = new API(client); } return mInstance; } private API(OkHttpClient client) { Retrofit retrofit = new Retrofit.Builder() .baseUrl(Constant.BASE_URL) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .client(client) .build(); mService = retrofit.create(APIService.class); }}
APIService封装
public interface APIService { @GET("bank/card/query") Call<CardResponse> getCardInfo(@Query("key") String key, @Query("card") String cardNumber);}
Model封装
- CardModel
public class CardModel { private String bank; private String bin; private int binNumber; private String cardName; private int cardNumber; private String cardType; //省略getter setter}
CardResponse
public class CardResponse { private String msg; private int retCode; private CardModel result; //省略getter setter}
查询
public static final String APP_KEY = "204f631d06000"; private void checkCardInfo() { //通过ApiService获取call请求对象 Call<CardResponse> cardInfoCall = API.getInstance().mService.getCardInfo(APP_KEY, CARD_NUM); //请求服务器 cardInfoCall.enqueue(new Callback<CardResponse>() { /** * 网络请求成功回调此方法 */ @Override public void onResponse(@Nullable Call<CardResponse> call, @Nullable Response<CardResponse> response) { if (response == null) { return; } //通过body方法获取服务器返回对象 CardResponse cardResponse = response.body(); if (cardResponse == null) { return; } //获取银行卡信息 CardModel result = cardResponse.getResult(); //展示数据 } /** * 网络请求失败时回调此方法,比如url不正确,请求超时等。 */ @Override public void onFailure(Call<CardResponse> call, Throwable t) { } }); }
请求示例截图
总结
其实,这些都是retrofit和okhttp的一些基本用法,okhttp还可以设置Interceptor(拦截器)用于添加header参数或者从服务器的返回头里获取信息,比如token, cookie等信息。另外还可以很方便地结合Rxjava来使用,这些我们都会在以后的博客中进行讲解。
源码下载
https://github.com/kuangxiaoguo0123/OkhttpExample
阅读全文
0 0
- Retrofit+okhttp基本使用
- android:retrofit+OKHttp使用
- Retrofit okhttp使用
- Retrofit+Rxjava+okhttp基本使用
- Retrofit、RxJava和OkHttp使用
- OKhttp+Retrofit
- 在安卓上使用RxJava Retrofit OKHttp GSON
- Retrofit使用OkHttp保存和添加cookie
- Retrofit和okhttp的了解和使用
- RxJava+Retrofit+OkHttp 懒人方式使用一
- 1、Retrofit+Okhttp的基本使用
- RxJava+Retrofit+OkHttp 懒人方式使用一
- Android使用Retrofit+OkHttp实现网络请求
- Okhttp插入日志及结合Retrofit使用
- 如何使用OkHttp/Retrofit重试HTTP请求
- Retrofit 2 okhttp 3 使用笔记
- Okhttp+Retrofit+Rxjava+MVP联合使用
- Retrofit和OkHttp的简单配合使用
- 单向链表-遍历(查找)
- python爬虫学习第二十八天
- 通过maven下载源码和javadoc方法
- Ubuntu解决eclipse CDT 不能识别C++11新特性问题
- where,having与 group by连用的区别
- Retrofit okhttp使用
- 树莓派3 B型刷入centos7及安装wifi模块
- solr搭建过程中控制台报错: Can't find (or read) directory to add to classloader: ../../../contrib/extraction/li
- 【特征检测】HOG特征算法
- Eclipse 开发工具基础配置
- Centos7.3安装vsftp服务
- Demo积累
- Angular基础(五) 内建指令和表单
- 管理学笔记-作为领导如何管理自己