1.build.gradle进行包依赖
implementation 'io.reactivex:rxjava:1.3.2'
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
implementation 'com.google.code.gson:gson:2.8.5'
2.创建RetrofitUtils
public class RetrofitUtils {
//TIMEOUT
public static final int TIMEOUT = 60;
private static volatile RetrofitUtils mInstance;
private OkHttpClient.Builder mOkHttpClient;
public static RetrofitUtils getInstance(){
if (null == mInstance) {
synchronized (RetrofitUtils.class) {
if (null == mInstance) {
mInstance = new RetrofitUtils();
}
}
}
return mInstance;
}
// public static RetrofitUtils getInstance(){
// return RetrofitUtilsHolder.INSTANCE;
// }
// private static class RetrofitUtilsHolder{
// private static final RetrofitUtils INSTANCE = new RetrofitUtils();
// }
public RetrofitUtils(){
initRetrofit();
}
private void initRetrofit() {
mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(TIMEOUT, TimeUnit.SECONDS)
.readTimeout(TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(TIMEOUT, TimeUnit.SECONDS);
if (BuildConfig.DEBUG){
//如果是Debug模式,则添加日志拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
mOkHttpClient.addInterceptor(httpLoggingInterceptor);
}
}
/**
* 创建API
*/
public <T> T create(Class<T> clazz,String baseUrl){
checkNotNull(baseUrl,"BaseUrl is Null--------------------");
return new Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(mOkHttpClient.build())
.build()
.create(clazz);
}
/**
* 检查对象是否为空
* */
private <T> T checkNotNull(T object, String s) {
if (object == null){
throw new NullPointerException(s);
}
return object;
}
}
3.根据接口数据进行GSONformat
public class JokeData {
/**
* reason : Success
* result : {"data":[{"content":"为了参加单位的迎新年联欢会,我在家里努力的练习着我最拿手的舞蹈 这时小侄女跑过来问我跳的是什么舞?? 我回:天鹅湖。。 小侄女白了我一眼撇撇嘴:\u201c我以为鸭子迷路跑我们家里来了!\u201d 我:。。。","hashId":"2a5f673682e2d6bbe393cce154fc285b","unixtime":1542945302,"updatetime":"2018-11-23 11:55:02"},{"content":"去朋友家做客,朋友很开心, 冲正在卧室兴致勃勃看喜羊羊灰太狼的儿子大喊: 宝宝,快去给叔叔倒杯水去! 我连忙说别那么客气,我不渴, 只见朋友瞬间变得一脸恨铁不成钢的哀怨表情: 没事,等这兔崽子把水拿来的时候,你会渴的! 我:。。。","hashId":"7f3a394495222f486670d084de22d819","unixtime":1542945302,"updatetime":"2018-11-23 11:55:02"},{"content":"六岁的小侄子竟然尿裤子了, 嫂子一边给他换裤子一边训斥他: 就在楼下玩了一会的功夫,还尿裤子!羞不羞啊你!你就不能憋那两分钟跑回家尿啊?! 小侄子立马提高了分贝反驳: 电视上说憋尿对肾不好!所以,我想尿就尿喽。。。","hashId":"6bcf29f06b280ed8bd1ad61fc62a9980","unixtime":1542945302,"updatetime":"2018-11-23 11:55:02"},{"content":"想生二胎,征求大女儿意见, 大女儿本来是拒绝的, 后来苦恼地和家有弟弟妹妹的同学咨询:\u201c让不让妈妈要二胎?\u201d 同学回复:\u201c为什么不让?有了弟弟妹妹就不会看管你这么严了。\u201d 大女儿果断同意了。","hashId":"976b7b281886bc49d0006c12b75459f6","unixtime":1542945302,"updatetime":"2018-11-23 11:55:02"},{"content":"坐公交遇一母子, 男孩就6。7岁左右。 天气冷,小孩要开窗户,打不开让他妈打开, 他妈说打不开没劲。 孩子说:你打我就有劲,让你开个窗户就没劲。","hashId":"79096a3b4a2fe5a1163e4f2f058c5b37","unixtime":1542945302,"updatetime":"2018-11-23 11:55:02"},{"content":"广播电台总是在节目当中插播广告,李大妈时常弄不清哪一部分是节目内容,哪一部分是广告。 这段时间,广播电台恰好为一家创伤医院做宣传,每天都要插播一则广告。 只听广告里一个女人大喊:\u201c司机,快,我儿子的手被机器轧断了!\u201d 司机马上接着说:\u201c别怕,咱们到创伤医院去!\u201d 李大妈听了,总是不满地抱怨:\u201c这女人怎么当妈的,孩子的手老是被机器轧断!\u201d","hashId":"8c0248c9d5c8c2991600123d242c0bd2","unixtime":1542945302,"updatetime":"2018-11-23 11:55:02"},{"content":"一个小偷一边和同伙喝酒,一边抱怨说:\u201c我今天真倒霉,眼睁睁看着一部新款手机在我面前晃荡了半天,还是没有狠下心来偷。\u201d 同伙很诧异:\u201c你平时胆子不是挺大的吗?怎么今天变小了?\u201d 小偷把酒杯往桌上一放,哭丧着脸说道:\u201c你以为我不想拿啊?可那人把手机音乐开得整条街都听得到,你偷给我看看\u2026\u2026\u201d","hashId":"818fc13a3609c503e570c5e5e18a44f7","unixtime":1542945302,"updatetime":"2018-11-23 11:55:02"},{"content":" 我一哥们他女朋友长相一般,去未来丈人家提亲时,未来丈母娘开口就要20W,少一分都不行!回来找我喝酒跟我抱怨,20w他也勉强凑得出来,他不爽的是,丈母娘要这20w,是他未来小舅子的丈母娘家也是要20W,而且他未来小舅子的女朋友是个美女,我当时脑抽就说一句,那你还不如拿这20w直接去娶你小舅子的女朋友呢!!他当时也只是讪讪的笑笑,后来。。。。。他真的做了,而且那小舅子的女朋友家还同意了!!只是现在他前女友家一直在跟他闹。","hashId":"5ece0717f2acde1f7caf4a87ea2da4dc","unixtime":1542942002,"updatetime":"2018-11-23 11:00:02"},{"content":"孩子:妈妈,你想要什么新年礼物? 妈妈:你唱首歌给妈妈吧。 孩子:再没有什么要说的了? 妈妈:我还应该说什么? 孩子:你应该问我想要什么礼物呀?","hashId":"7b11e79df636ad72cbadd1bc20b2bc37","unixtime":1542941702,"updatetime":"2018-11-23 10:55:02"},{"content":"领导去汨罗视察,当地旅游局负责接待。走到一座山下,导游对领导说:\u201c前面有一处名胜。\u201d 领导问:\u201c什么名胜?\u201d 导游说:\u201c是一口泉,屈原泉。\u201d 领导又问了一遍:\u201c什么泉?\u201d 导游复述说:\u201c屈\u2014\u2014原\u2014\u2014泉。\u201d 领导怒道:\u201c就你懂拼音!\u201d","hashId":"ebaad86e9d8d5fdaa0b7e7257004f1cd","unixtime":1542941702,"updatetime":"2018-11-23 10:55:02"}]}
* error_code : 0
*/
private String reason;
private ResultBean result;
private int error_code;
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public int getError_code() {
return error_code;
}
public void setError_code(int error_code) {
this.error_code = error_code;
}
public static class ResultBean {
private List<DataBean> data;
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
/**
* content : 为了参加单位的迎新年联欢会,我在家里努力的练习着我最拿手的舞蹈 这时小侄女跑过来问我跳的是什么舞?? 我回:天鹅湖。。 小侄女白了我一眼撇撇嘴:“我以为鸭子迷路跑我们家里来了!” 我:。。。
* hashId : 2a5f673682e2d6bbe393cce154fc285b
* unixtime : 1542945302
* updatetime : 2018-11-23 11:55:02
*/
private String content;
private String hashId;
private int unixtime;
private String updatetime;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getHashId() {
return hashId;
}
public void setHashId(String hashId) {
this.hashId = hashId;
}
public int getUnixtime() {
return unixtime;
}
public void setUnixtime(int unixtime) {
this.unixtime = unixtime;
}
public String getUpdatetime() {
return updatetime;
}
public void setUpdatetime(String updatetime) {
this.updatetime = updatetime;
}
}
}
}
4.创建接口ServerApi
public interface ServerApi {
/*@GET("toutiao/index?key=d78b502268f7456b79fbe7228cecdd46")
Observable<NewsData> getNewsData(
@Query("type") String type
);*/
@POST("text.from?key=ae240f7fba620fc370b803566654949e")
Observable<JokeData> getCurrentJokeData(
@Query("page") int page,
@Query("pagesize") int pagesize
);
}
5.配置Config网址
public class RxConfig {
public static final String BASE_JOKE_URL = "http://japi.juhe.cn/joke/content/";
public static final String BASE_LOGIN_URL = "http://japi.juhe.cn/joke/content/";
}
6.数据获取
//每次生成一次Disposable,添加到compositeDisposable,页面退出时候记得清除掉,不然可能会造成内存泄漏
new Thread(new Runnable() {
@Override
public void run() {
Disposable mDisposable = RetrofitUtils.getInstance().create(ServerApi.class, RxConfig.BASE_JOKE_URL)
.getCurrentJokeData(2,10)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<JokeData>() {
@Override
public void call(JokeData jokeData) {
Log.d("csdn","data---"+jokeData.getResult().getData().toString());
}
});
if (compositeDisposable != null){
compositeDisposable.add(mDisposable);
}
}
}).start();
public abstract class BaseActivity extends Activity {
protected CompositeDisposable compositeDisposable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
compositeDisposable = new CompositeDisposable();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (compositeDisposable != null){
compositeDisposable.clear();
compositeDisposable = null;
}
}
}