使用Retrofit和RxJava
使用Retrofit和RxJava整合访问网络,然后将数据显示到界面上
def retrofitVersion = '2.0.0-beta1' dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1' //Retrofit
compile "com.squareup.retrofit:retrofit:$retrofitVersion"
compile "com.squareup.retrofit:converter-gson:$retrofitVersion" //RxJava
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'
}
在app.buidle里面添加ReTrofit和RxJava的依赖,在
dependencies上面一定要注明Retrofit的版本号
def retrofitVersion = '2.0.0-beta1' MainActivity里面的代码:
public class MainActivity extends Activity implements View.OnClickListener {
/**
* 自定义的观察者
*/
public MyObserver observer; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this); observer = new MyObserver();//创建一个观察者 }
//点击按钮就请求网络
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn) {//点击获取好友动态
Control control = new Control(this);
control.getFriendsShareFromServer();//访问网络并且解析Json }
} /**
* 自定义的观察者
*/
class MyObserver implements rx.Observer<Resquest_friends_info> { @Override
public void onCompleted() {
Log.d("msg", "观察的事件结束了---");
} @Override
public void onError(Throwable e) {
Log.d("msg", "观察的事件出错了");
}
//订阅观察者后,被观察者会把数据传回来
@Override
public void onNext(Resquest_friends_info resquest_friends_info) {
Log.d("msg", "观察者OnNext");
ArrayList<Resquest_friends_info.EveryShareInfo> results = resquest_friends_info.getResult();
Toast.makeText(MainActivity.this,"观察者收到了数据",Toast.LENGTH_SHORT).show(); for (Resquest_friends_info.EveryShareInfo info : results) {//每条分享的信息
Log.d("msg", "分享信息++++" + info.getPub_context() + "--->" + info.getPub_datetime() + "----->" + info.getPub_frd_name());
Log.d("msg", "-----------------------------------------------------"); for (Resquest_friends_info.EveryShareInfo.Reply reply : info.pub_com) {//每条回复
Log.d("msg", "评论+++++" + reply.getPc_name() + "--->" + reply.getPc_txt() + "--->");
Log.d("msg", "----------------------------------------------------------------");
}
for (Resquest_friends_info.EveryShareInfo.Thumb thumb : info.pub_thumup) {//每个点赞
Log.d("msg", "点赞++++" + thumb.getPt_name());
Log.d("msg", "---------------------------------------");
}
}
}
}
}
Resquest_friends_info表示一个JavaBean对象,
app结构如下:
//访问网络的接口
public interface GitHubService {
// ================================================ = = == ==========
//
//表示Get请求,里面是地址这是写死的地址,但是地址中的参数要动态改变,就不能这样写
// @GET("/index.php?m=home&c=message&a=resquest_friends_info&uid=1234567&p=1")
//参数要动态传进去,所以要这样写
//@GET(value = "/index.php"), 或者@GET("/index.php")也可以
@GET("/index.php")
//用这个方法去访问网络
Call<Resquest_friends_info> getFriendsShareInfo(@Query("m") String m,@Query("c") String c,@Query("a") String a,@Query("uid") String uid,@Query("p") String p);
} 这里的路径可以自己修改,比如,写成这样也是可以的:
//@GET("/index.php?m=home")
//Call<Resquest_friends_info> getFriendsShareInfo(@Query("c") String c,@Query("a") String a,@Query("uid") String uid,@Query("p") String p);
/**此类是访问网络的Control
* Created by xhj on 15-12-18.
*/
public class Control {
public static final String TAG="msg";
//URL根路径,一般就是域名
public static final String APITrack="http://192.168.1.102";
private MainActivity activity;
/**构造方法时把观察者所在的类传进来*/
public Control(MainActivity activity){
this.activity=activity;
}
/**从服务器获取好友动态*/
public void getFriendsShareFromServer(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APITrack)
.addConverterFactory(GsonConverterFactory.create())//用Gson去解析数据
.build();
GitHubService git = retrofit.create(GitHubService.class);
Call<Resquest_friends_info> call = git.getFriendsShareInfo();
call.enqueue(new Callback<Resquest_friends_info>() {
//访问网络回来,并且成功拿到数据就调用这个方法
@Override
public void onResponse(Response<Resquest_friends_info> response) {
final Resquest_friends_info resquest_friends_info = response.body(); Observable<Resquest_friends_info> observable = Observable.create(new Observable.OnSubscribe<Resquest_friends_info>() {
@Override
public void call(Subscriber<? super Resquest_friends_info> subscriber) {
subscriber.onNext(resquest_friends_info);
subscriber.onCompleted();//事件结束
}
});
observable.subscribe(activity.observer);//订阅观察者 } @Override
public void onFailure(Throwable t) {
Log.d("msg","失败了");
}
}); }
}
使用Retrofit和RxJava的更多相关文章
- Retrofit结合RxJava使用指南
Retrofit结合RxJava使用指南 Retrofit是一个当前很流行的网络请求库, 官网的介绍是: "Type-safe HTTP client for Android and Jav ...
- Android okHttp网络请求之Retrofit+Okhttp+RxJava组合
前言: 通过上面的学习,我们不难发现单纯使用okHttp来作为网络库还是多多少少有那么一点点不太方便,而且还需自己来管理接口,对于接口的使用的是哪种请求方式也不能一目了然,出于这个目的接下来学习一下R ...
- Retrofit与RXJava整合
Retrofit 除了提供了传统的 Callback 形式的 API,还有 RxJava 版本的 Observable 形式 API.下面我用对比的方式来介绍 Retrofit 的 RxJava 版 ...
- Android笔记之Retrofit与RxJava的组合
依赖 implementation 'com.squareup.retrofit2:retrofit:2.5.0' implementation 'com.squareup.retrofit2:con ...
- Retrofit+Okhttp+RxJava打造网络请求之Post
之前一直在准备Android培训的事情,所幸的是终于完事啦,在这过程中真的发现了自身无论从沟通能力还是技术能力上很多的不足,就用一句 路漫漫其修远兮,吾将上下而求索 来勉励自己吧.之前也在项目上用上O ...
- Retrofit与RXJava整合(转)
Retrofit 除了提供了传统的 Callback 形式的 API,还有 RxJava 版本的 Observable 形式 API.下面我用对比的方式来介绍 Retrofit 的 RxJava 版 ...
- Android--带你一点点封装项目 MVP+BaseActivity+Retrofit+Dagger+RxJava(二)
1,昨天我们基本上把MVP给封装起来了,今天接着昨天的东西来结合RxJava把Retrofit把网络框架简单的封装一下,先看一下我们今天实现的效果: 哈哈 ,还是昨天的效果,好吧 ,我认错. 2,由于 ...
- Android--带你一点点封装项目 MVP+BaseActivity+Retrofit+Dagger+RxJava(一)
1,其实早就想把这些东西给封装封装的,一直没有时间,今天刚好项目进入到测试阶段了,Bug同事在哪儿测试的飞起,但发现提bug的尽然是我(得意脸),然后上午把ios的包测试了一下,顺便把服务器给测挂了( ...
- retrofit okhttp RxJava bk Gson Lambda 综合示例【配置】
项目地址:https://github.com/baiqiantao/retrofit2_okhttp3_RxJava_butterknife.git <uses-permission andr ...
随机推荐
- F - 娜娜梦游仙境系列——多民族王国
F - 娜娜梦游仙境系列——多民族王国 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Othe ...
- dd if=/dev/zero of=的含义是什么?Linux 下的dd命令使用详解
http://blog.sina.com.cn/s/blog_8b5bb24f01016y3o.html 一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 注意:指 ...
- docker network
前言:前面的部分一直都是单机跑docker,但实际生产环境不可能只用一台来跑.肯定会用到多台,因为他们都是内部私有ip,那么多台主机之间的容器如何通信?这个是个很头疼的问题!目前主流几种方法如下: 1 ...
- Sql Sever语句 (续2)
日期时间 把日期类型的字段下默认值或绑定里的 设置成getdate() ,新建字段时候不设置会自动获取当前服务器时间 在当前时间加上xx年xx月xx天 select datedd(yy,100,g ...
- 《JavaScript高级程序设计》读书笔记 ---基本概念小结
ECMAScript 中的基本数据类型包括Undefined.Null.Boolean.Number 和String. 与其他语言不同,ECMScript 没有为整数和浮点数值分别定义不同的数据 ...
- 【转】解决Windows不能在本地计算机启动apache tomcat
http://blog.163.com/ftskwsg@126/blog/static/5623853020094494117827/ 这个方法解决了我的问题. 在windows下以服务的方式启动时提 ...
- ASP.NET MVC 使用带有短横线的html Attributes
我们常常需要一个文本框来输入用户名,在asp.net mvc 中可以使用 TextBoxFor.有时候我们想为这个文本框添加一些特性,比如 html5 的data-,如果直接写data-是会提示错误的 ...
- Linux系统监控实用工具Glances
Linux系统监控实用工具Glances Glances安装 Glances安装要求:python >= 2.6 和 psutil >= 0.4.1 1.第一步,安装了python-> ...
- 给EditText设置边框
布局文件中加入background属性: <EditText android:layout_width="200dp" android:layout_height=" ...
- listener、context、filter、servlet及其加载顺序
首先说加载顺序:context-param—>listener —> filter —> servlet 这四类加载顺序与配置顺序无关,对于每一类内部的加载顺序,与配置顺序有关: l ...