Retrofit工具类
package com.example.week2.retrofitUtils;
import android.util.Log;
import com.example.week2.model.Contacts;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.www.gcyL157.com TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.www.tongqt178.com adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
/**
苏青青:
作者:${lvf}
创建时间:${2018}${12}$
更改时间:2019/1/620:53
版本号:1
*/
public class Retofit {
private MyApiService myApiService1;
private Retofit(){
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient =www.meiwanyule.cn/ new OkHttpClient.Builder()
.readTimeout(20, TimeUnit.SECONDS)
.connectTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.retryOnConnectionFailure(true)
.build();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(Contacts.BASE_URL)
.client(okHttpClient)
.build();
myApiService1 = retrofit.create(MyApiService.class);
}
public static Retofit getInstance() {
return RetroHolder.retro;
}
private static class RetroHolder {
private static final Retofit retro = new Retofit();
}
public void get(String url, Map<String,String> map, final HttpListener listener){
Observer observer = new Observer<ResponseBody>() {
@Override
public void onCompleted() {
Log.e("onCompleted","onCompleted");
}
//网络处理失败
@Override
public void onError(Throwable e) {
Log.e("onError","onError"+e.getMessage());
if (listener != null) {
listener.onError(e.getMessage());
}
}
//网络处理成功
@Override
public void onNext(ResponseBody responseBody) {
Log.d("onNext","onNext");
if (listener != null) {
try {
listener.onSuccess(responseBody.string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
myApiService1.get(url, map)
.subscribeOn(Schedulers.io())//io就是子线程
//在主线程调用
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
public void post(String url,Map<String,String> map,final HttpListener listener){
Observer observer = new Observer<ResponseBody>() {
@Override
public void onCompleted() {
Log.e("onCompleted","onCompleted");
}
//网络处理失败
@Override
public void onError(Throwable e) {
Log.e("onError","onError"+e.getMessage());
if (listener != null) {
listener.onError(e.getMessage( www.qinlinyu.cn/ ));
}
}
//网络处理成功
@Override
public void onNext(ResponseBody responseBody) {
Log.d("onNext","onNext");
if (listener != null) {
try {
listener.onSuccess(responseBody.string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
myApiService1.post(url, map)
.subscribeOn(Schedulers.io())//io就是子线程
//在主线程调用
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}
public interface HttpListener {
void onSuccess(String jsonStr);
void onError(String error);
//Contacts类
package com.example.asus.bw_shop;
/**
苏青青:
作者:${lvf}
创建时间:${2018}${12}$
更改时间:2018/12/2919:59
版本号:1
*/
public class Contacts {
//总接口前缀
public static final String BASE_URL = “http://mobile.bwstudent.com/small/”;
//登陆
public static final String USER_REGISTER = “user/v1/register”;
//用户信息
public static final String USER_LOGIN = “user/v1/login”;
//轮播图
public static final String FLY_BANNER = "commodity/v1/bannerShow";
public static final String SY_SHOP= "commodity/v1/commodityList";
//圈子列表
public static final String CIRCLE_LIST= "circle/v1/findCircleList";
//商品列表加载更多
public static final String SHOP_MORE= "commodity/v1/findCommodityListByLabel";
public static final String KEY_SHOP= "commodity/v1/findCommodityByKeyword";
//MyApiService接口
package com.example.asus.bw_shop.utils;
import java.util.Map;
import okhttp3.ResponseBody;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.QueryMap;
import retrofit2.http.Url;
import rx.Observable;
/**
苏青青:
作者:${lvf}
创建时间:${2018}${12}$
更改时间:2018/12/2920:05
版本号:1
*/
public interface MyApiService {
@GET
Observable<ResponseBody> get(@Url String url, @QueryMap Map<String,String> map);
@POST
Observable<ResponseBody> post(@Url String url, @QueryMap Map<String,String> map);
//Model接口
package com.example.week2.model;
import java.util.Map;
创建时间:${2018}${12}$
更改时间:2018/12/308:58
版本号:1
*/
public interface Model {
void Request(String url, Map<String, String> map, Class cla, MyCallBack myCallBack);
Retrofit工具类的更多相关文章
- Retrofit+RxJava联网工具类
在python 进程.线程 (一)中简单的说过,CPython中的GIL使得同一时刻只能有一个线程运行,即并发执行.并且即使是多核CPU,GIL使得同一个进程中的多个线程也无法映射到多个CPU上运行, ...
- Android开源项目发现--- 工具类快速开发篇(持续更新)
1. Guava Google的基于java1.6的类库集合的扩展项目 包括collections, caching, primitives support, concurrency librarie ...
- Android开源项目发现--- 工具类数据库ORM篇(持续更新)
orm的db工具类,简化建表.查询.更新.插入.事务.索引的操作 1. greenDAO Android Sqlite orm的db工具类 项目地址:https://github.com/greenr ...
- Android开源项目发现--- 工具类网络相关篇(持续更新)
1. Asynchronous Http Client for Android Android异步Http请求 项目地址:https://github.com/loopj/android-async- ...
- Android开源项目大全 - 工具类
主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少Vie ...
- Android基于Retrofit2.0 +RxJava 封装的超好用的RetrofitClient工具类(六)
csdn :码小白 原文地址: http://blog.csdn.net/sk719887916/article/details/51958010 RetrofitClient 基于Retrofit2 ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
随机推荐
- springboot 配置二级缓存
springBoot中配置mybatis的二级缓存 2018年01月22日 11:45:37 Ting.Xue(Martin.Xue) 阅读数:5604更多 个人分类: SSM的Spring框架Myb ...
- FreeRTOS信号量的封装函数参数是二级指针
1. 先看正确的封装方式,问题所在,为什么要用2级指针 void cissys_lockcreate(void** mutex) { //创建信号量,应该是互斥锁 *mutex = ((Semapho ...
- Ubuntu16.04比较好的一系列软件安装介绍
https://blog.csdn.net/Gerald_Jones/article/details/80784976
- Cannot assign requested address (connect failed)
压测时,应用服务器报错:Cannot assign requested address (connect failed) 经检查,由于应用服务器,频繁发起http请求,由于每次连接都在很短的时间内结束 ...
- TW实习日记:第16天
前端的样式bug实在是太太太莫名其妙了,尤其是封装好的组件,一层套一层的,根本不知道是哪一层出了问题...除了改bug就是做新功能,真想吐槽一下这个项目的留言板,根本没人会用吧...这功能实在是太老旧 ...
- lintcode142 O(1)时间检测2的幂次
O(1)时间检测2的幂次 用 O(1) 时间检测整数 n 是否是 2 的幂次. 您在真实的面试中是否遇到过这个题? Yes 样例 n=4,返回 true; n=5,返回 false. 二进制的n中只有 ...
- 《Effective C++》读书笔记 条款03 尽可能使用const 使代码更加健壮
如果你对const足够了解,只需记住以下结论即可: 将某些东西声明为const可帮助编译器侦测出错误用法,const可被施加于任何作用于内的对象.函数参数.函数返回类型.成员函数本体. 编译器强制实施 ...
- python SyntaxError: Non-ASCII character '\xe8' in file C:\Users\nwpujun\PycharmProjects\projects\hrl1\hrlAgent\src\li_nn.py on line 52
解决方法:在文件头部加上这样的一句话 # -*- coding: utf-8 -*- 注意:加在其他的位置可能没用,我就是这样的
- POJ 1921 Paper Cut(计算几何の折纸问题)
Description Still remember those games we played in our childhood? Folding and cutting paper must be ...
- 20145214 《Java程序设计》第7周学习总结
20145214 <Java程序设计>第7周学习总结 教材学习内容总结 时间的度量 格林威治标准时间(GMT),现已不作为标准时间使用,即使标注为GMT(格林威治时间),实际上谈到的的是U ...