Retrofit的通讯方式示例
Retrofit有两种通讯方式,同步和异步
异步方式:
APIService req;
req = RetrofitManager.getInstance().createReq(APIService.class);
String deviceId=DeviceUtil.GetDeviceId(context);
Call<ResponseBody> res= req.getCpDatas(GetBaoCanReqPara(deviceId,fromId));
res.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.e("", "");
try {
同步方式:
APIService req;
req = RetrofitManager.getInstance().createReq(APIService.class);
try{
Call<ResponseBody> res= req.CheckSignState(hashMap);
final Response<ResponseBody> execute = res.execute();
String json=execute.body().string();
封装类库:
import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.GsonConverterFactory;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; public class RetrofitManager {
private static RetrofitManager mRetrofitManager;
private Retrofit mRetrofit; private RetrofitManager(){
initRetrofit();
} public static synchronized RetrofitManager getInstance(){
if (mRetrofitManager == null){
mRetrofitManager = new RetrofitManager();
}
return mRetrofitManager;
} private void initRetrofit() {
HttpLoggingInterceptor LoginInterceptor = new HttpLoggingInterceptor();
LoginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder builder = new OkHttpClient.Builder(); if (AppConfig.DEBUG){
builder.addInterceptor(LoginInterceptor); //添加retrofit日志打印
} builder.connectTimeout(15, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
builder.retryOnConnectionFailure(true);
OkHttpClient client = builder.build(); mRetrofit = new Retrofit.Builder()
.baseUrl(AppConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())//用于返回Rxjava调用,非必须
.client(client)
.build();
} public <T> T createReq(Class<T> reqServer){
return mRetrofit.create(reqServer);
}
}
Retrofit的通讯方式示例的更多相关文章
- 前后端常用通讯方式-- ajax 、websocket
一.前后端常用通讯方式 1. ajax 浏览器发起请求,服务器返回数据,服务器不能主动返回数据,要实现实时数据交互只能是ajax轮询(让浏览器隔个几秒就发送一次请求,然后更新客户端显示.这种方式实际 ...
- 【工业串口和网络软件通讯平台(SuperIO)教程】九.重写通讯接口函数,实现特殊通讯方式
SuperIO相关资料下载:http://pan.baidu.com/s/1pJ7lZWf 1.1 统一的IO接口 开发一套设备驱动同时具备串口和网络通讯能力,通讯接口在逻辑上是统一的,在此基础 ...
- Angular1.x组件通讯方式总结
Angular1开发模式 这里需要将Angular1分为Angular1.5之前和Angular1.5两个不同的阶段来讲,两者虽然同属Angular1,但是在开发模式上还是有较大区别的.在Angula ...
- android ipc通信机制之之三,进程通讯方式。
IPC通讯方式的优缺点: IPC通讯方式的对比 名称 优点 缺点 适用场景 Bundle 简单易用 只能传输Bundle支持的数据类型 四大组件的进程通信 文件共享 简单易用 不适合高并发场景,并无法 ...
- [Linux]PHP-FPM与NGINX的两种通讯方式
一.通过监听TCP端口通讯 php-fpm.d/www.conf ; The address on which to accept FastCGI requests. ; Valid syntaxes ...
- TCP UDP Socket 即时通讯 API 示例 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Angular1组件通讯方式总结
这里需要将Angular1分为Angular1.5之前和Angular1.5两个不同的阶段来讲,两者虽然同属Angular1,但是在开发模式上还是有较大区别的.在Angular1.4及以前,主要是基于 ...
- Tuxedo 通讯方式解析
本节根据tuxedo自带samples的例子,让其运行起来.并通过这个例子,深入的理解tuxedo的通讯方式. 进入tuxedo的安装目录,samples目录下自带了一些例子 [root@localh ...
- Linux 进程间通讯方式 pipe()函数 (转载)
转自:http://blog.csdn.net/ta893115871/article/details/7478779 Linux 进程间通讯方式有以下几种: 1->管道(pipe)和有名管道( ...
随机推荐
- 【HDU1846】Brave Game(博弈论)
题面 HDU 题解 \(Bash\ Game\)模板题 #include<iostream> using namespace std; int T,n,m; int main() { io ...
- Python中生成器generator和迭代器Iterator的使用方法
一.生成器 1. 生成器的定义 把所需要值得计算方法储存起来,不会先直接生成数值,而是等到什么时候使用什么时候生成,每次生成一个,减少计算机占用内存空间 2. 生成器的创建方式 第一种只要把一个列表生 ...
- BZOJ 1042: [HAOI2008]硬币购物 (详解)(背包&容斥原理)
题面:https://www.cnblogs.com/fu3638/p/6759919.html 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚 ...
- css 圆形头像
方法一:背景图片(推荐) 好处是,图片长宽不等的情况下图片不会变形 .ui-photo { width: 100px; height: 100px; background: url("img ...
- A1119. Pre- and Post-order Traversals
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...
- vetur插件提示 'v-for' directives require 'v-bind:key' directives.错误的解决办法
在用vscode编写vue代码时,因为安装的有vetur插件,所以当代码中有v-for语法时,会提示 [vue-language-server] 'v-for' directives require ...
- jQuery的on绑定事件在mobile safari(iphone / ipad / ipod)上无法使用的解决方案
用一个div当做了一个按钮来使用. <div class="button"> <div class=" next_button button_left ...
- 8个Python小Tips
原创: 金牌小编 行走在Python的江湖,那能不收藏一些锦囊妙计,今天分享给大家8个小技巧,希望在实战的时候有帮助! 01. 条件判断 condition=Trueif condition: ...
- 2017-12-14python全栈9期第一天第二节之初始计算机系统
CPU:相当于人的大脑.用于计算 内存:储存数据.4G.8G.32G....成本高.断电即消失 硬盘:固态.机械.长久保存数据+文件 操作系统: 应用程序:
- u-boot(二)makefile
目录 u-boot(二)makefile 引入 目录结构(1.1.6) 配置文件 目标 配置具体的单板 编译阶段 过程 链接入口 配置链接地址 附录 附录A:mkconfig解析 附录B 链接脚本 t ...