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)和有名管道( ...
随机推荐
- BUG关闭原因
已解决:缺陷已经修复. 重复缺陷:是指在系统里相同原因的缺陷已经被其他人报告.在此缺陷被作为重复缺陷返回时,先不要立即取消.必须等到核查修复后,才在系统里取消.这是因为有些缺陷被误认为是重复缺陷,实际 ...
- bzoj1014 火星人 (hash+splay+二分答案)
求公共前缀的问题可以用hash+二分来解决,但这个是动态的,所以我们用平衡树来维护区间的hash值 复杂度$O(mlog^2n)$ #include<bits/stdc++.h> #def ...
- centos7安装较高版本python3.5/3.6
应用环境: Centos7或者RHEL7下默认安装的python版本为2.7.x,更新不够及时,现在很多时候需要额外安装较高版本的python环境, 网上搜罗一圈总结记录一下常用两种方式: ① 源码编 ...
- Nio再学习之NIO的buffer缓冲区
1. 缓冲区(Buffer): 介绍 我们知道在BIO(Block IO)中其是使用的流的形式进行读取,可以将数据直接写入或者将数据直接读取到Stream对象中,但是在NIO中所有的数据都是使用的换冲 ...
- MySQL命令,一篇文章替你全部搞定
MySQL命令,一篇文章替你全部搞定 MySQL的基本操作可以包括两个方面:MySQL常用语句如高频率使用的增删改查(CRUD)语句和MySQL高级功能,如存储过程,触发器,事务处理等.而这两个方面又 ...
- Developing JSF applications with Spring Boot
Developing JSF applications with Spring Boot Spring Boot can leverage any type of applications, not ...
- JBoss/Wildfly 配置SQLserver服务器
JBoss/Wildfly 配置SQLserver服务器 http://blog.csdn.net/haitaolang/article/details/60467118 wildfly standa ...
- cf 990G - GCD Counting
题意 #include<bits/stdc++.h> #define t 200000 #define MAXN 200100 using namespace std; int n; in ...
- 洛谷P1712 区间
题意:给你n个区间,从中选择m个,使得它们有交,且最长与最短区间的差值最小. 解:这道题我想了好多的,nlog²n错的,nlogn错的,最后终于想出nlogn的了...... 把区间按照长度排序,然后 ...
- /etc/profile
PS1:就是用户平时的提示符. PS2:第一行没输完,等待第二行输入的提示符. Linux系统提示符是用系统变量PS1来定义的.一般系统默认的形式是:[username@host 工作目录]$. 用e ...