更改 Retrofit解析网页Json数据简单实例,将Retrofit中的回调方式改为RxJava方式

dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:23.4.0'compile 'com.squareup.retrofit2:converter-gson:2.1.0'compile 'io.reactivex:rxjava:1.0.14'compile 'io.reactivex:rxandroid:1.0.1'compile 'com.squareup.picasso:picasso:2.5.2'compile 'com.jakewharton:butterknife:8.2.1'apt 'com.jakewharton:butterknife-compiler:8.2.1'compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'}
package com.android.retrofitdemo;import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
import rx.Observable;/*** 获取新闻列表*/
public interface NewsService {/*@GET("/api/cook/list" )Call<Tngou> getNews(@Query("id") int id, @Query("page") int page, @Query("rows") int rows);*/@GET("/api/cook/list")Observable<Tngou> getNews(@Query("id") int id, @Query("page") int page, @Query("rows") int rows);
}
package com.android.retrofitdemo;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;import java.util.ArrayList;
import java.util.List;import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subjects.Subject;public class MainActivity extends AppCompatActivity{@BindView(R.id.news_listView)ListView newsListView;private NewsAdapter adapter;private List<News> newsList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.bind(this);Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.tngou.net").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).addConverterFactory(GsonConverterFactory.create()).build();NewsService newsService = retrofit.create(NewsService.class);//Call<Tngou> call = newsService.getNews(0,1,20);//call.enqueue(this);newsService.getNews(0,1,20).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Tngou>() {@Overridepublic void onCompleted() {}@Overridepublic void onError(Throwable e) {}@Overridepublic void onNext(Tngou tngou) {newsList = tngou.getNewsList();adapter.addAll(newsList);}} );ListView newsListView = (ListView) findViewById(R.id.news_listView);adapter = new NewsAdapter(this, new ArrayList<News>());newsListView.setAdapter(adapter);}/*@Overridepublic void onResponse(Call<Tngou> call, Response<Tngou> response) {newsList = response.body().getNewsList();adapter.addAll(newsList);}@Overridepublic void onFailure(Call<Tngou> call, Throwable t) {t.printStackTrace();}*/
}

Retrofit+RxJava相关推荐

  1. Android Retrofit+RxJava 优雅的处理服务器返回异常、错误

    Android Retrofit+RxJava 优雅的处理服务器返回异常.错误 参考文章: (1)Android Retrofit+RxJava 优雅的处理服务器返回异常.错误 (2)https:// ...

  2. java创建请求拦截器_80.简单Retrofit+RxJava+日志拦截器结合使用

    1.需要使用到的依赖如下(Retrofit\RxJava\RecyclerView\日志拦截器) //only Retrofit(只用Retrofit联网) implementation 'io.re ...

  3. kotlin for android----------MVP模式下(OKHttp和 Retrofit+RxJava)网络请求的两种实现方式...

    2019独角兽企业重金招聘Python工程师标准>>> 今天要说的干货是:以Kotlin,在MVP模式下(OKHttp和 Retrofit+RxJava)网络请求两种实现方式的一个小 ...

  4. 教你分分钟使用Retrofit+Rxjava实现网络请求

    撸代码之前,先简单了解一下为什么Retrofit这么受大家青睐吧. Retrofit是Square公司出品的基于OkHttp封装的一套RESTful(目前流行的一套api设计的风格)网络请求框架.它内 ...

  5. 分分钟使用Retrofit+Rxjava实现网络请求

    撸代码之前,先简单了解一下为什么Retrofit这么受大家青睐吧.??? Retrofit是Square公司出品的基于OkHttp封装的一套RESTful(目前流行的一套api设计的风格)网络请求框架 ...

  6. Retrofit+Rxjava服务器IP轮询重试机制实现

    为了保证客户端稳定性和网络容错性,提升用户体验,在客户端建立服务端IP轮询及重试机制是十分有必要的,本文为我个人经验总结,希望对你能有参考价值. app在启动时会请求一些配置信息,其中就包括ip的路由 ...

  7. Android MVP开发模式及Retrofit + RxJava封装

    代码已上传到Github,因为接口都是模拟无法进行测试,明白大概的逻辑就行了! 欢迎浏览我的博客--https://pushy.site 1. MVP模式 1.1 介绍 如果熟悉MVP模式架构的话,对 ...

  8. kotterKnife+dagger2+otto+okhttp+picasso+retrofit+rxjava示例项目,使用kotlin编写。

    kotterKnife+dagger2+otto+okhttp+picasso+retrofit+rxjava示例项目,使用kotlin编写. 链接:https://github.com/zengzh ...

  9. java 返回ro,错误retrofit rxjava优雅的处理服务器返回异常、错误

    Retrofit+RxJava优雅的处理服务器 返 回异常.错误 异常&错误 实际开发经常有这种情况比如登录请求接口返回的信息包括请求返回的状态失败还是成功错误码 Us er对象等等 ...

  10. Retrofit + RxJava 的结合使用

    Retrofit + RxJava火了好一阵子了,网上也有很多博客,下面对之前的学习经验做一些总结,权当记笔记了,若是能够帮到看到的人,那也是很高兴的. 对于Retrofit不熟悉,可以查看 Retr ...

最新文章

  1. java(x ) 3的值_java中,设x=2,则表达式 (x++)*3的值是多少 设x=2则表达式(x+
  2. oracle的39082,ORA-39082 导入数据遇到的问题
  3. 10_Android中通过HttpUrlConnection访问网络,Handler和多线程使用,读取网络html代码并显示在界面上,ScrollView组件的使用
  4. python比较日期大小_Python日期的处理——datetime模块
  5. 一步步编写操作系统 37 一级页表与虚拟地址2
  6. Python 数据分析三剑客之 NumPy(六):矩阵 / 线性代数库与 IO 操作
  7. /proc/net/tcp中各项参数说明
  8. Web前端——CSS层叠样式表
  9. vs2010编译curl为static库及测试
  10. Hinton神经网络公开课16 Recent applications of deep neural nets
  11. Unity中文博主频道列表
  12. java 自动化测试_java写一个自动化测试
  13. MATLAB中pdetool工具求解泊松方程和Laplace方程
  14. 除了php云和骑士还有什么_PHP云人才系统 VS 骑士cms人才系统
  15. 【历史】 apache catalina servlet tomcat 命名的由来
  16. 【 C++ 技术】 C++ 高性能服务器网络框架设计细节
  17. Webstorm2018激活和汉化
  18. ionic4的ion-searchbar的setFocus()方法使用
  19. 220514西南科技正式赛补题
  20. 微分方程的数值解法与符号解法

热门文章

  1. SVM学习(一):SVM概念
  2. 机器视觉:Asymmetry Problem in Computer Vision
  3. 多线程之线程池的应用
  4. 深度学习(二十四)矩阵分解之基于k-means的特征表达学习
  5. 用Python Pandas处理亿级数据
  6. Go 语言简介(下)— 特性
  7. nginx2goaccess.sh脚本内容
  8. Ajax请求导出Excel的问题
  9. SQL_由创建表引出
  10. Cassandra HBase和MongoDb性能比较