retrofit与rx_java结合使用基础篇之一
来源:互联网 发布:关节机器人编程 编辑:程序博客网 时间:2024/06/09 18:55
1、导入五个依赖,前面两个是retrofit的,中间那个是retrofit与rxjava结合使用需要的Adapter,后面两个是rx_java需要的。
compile 'com.squareup.retrofit2:retrofit:2.1.0'compile 'com.squareup.retrofit2:converter-gson:2.1.0'compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'io.reactivex:rxandroid:1.1.0'compile 'io.reactivex:rxjava:1.1.0'
2、需要的接口:
public interface GitHubService { @GET("/repos/{owner}/{repo}/contributors") Observable<List<Contributor>> GetGitHubData(@Path("owner") String owner, @Path("repo") String repo);}
3、实体类:
public class Contributor { @SerializedName("login") String login; @SerializedName("id") int id; @SerializedName("avatar_url") String avatar_url; @SerializedName("gravatar_id") String gravatar_id; @SerializedName("url") String url; @SerializedName("html_url") String html_url; @SerializedName("followers_url") String followers_url; @SerializedName("following_url") String following_url; @SerializedName("gists_url") String gists_url; @SerializedName("starred_url") String starred_url; @SerializedName("subscriptions_url") String subscriptions_url; @SerializedName("organizations_url") String organizations_url; @SerializedName("repos_url") String repos_url; @SerializedName("events_url") String events_url; @SerializedName("received_events_url") String received_events_url; @SerializedName("type") String type; @SerializedName("site_admin") String site_admin; @SerializedName("contributions") int contributions;}
4、MainActivity的调用代码:
public class MainActivity extends AppCompatActivity { private static final String TAG = "gitHub"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DownLoadData(); } private void DownLoadData() { new Thread() { @Override public void run() { Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com") .addConverterFactory(GsonConverterFactory.create()) .build(); GitHubService gitHubService = retrofit.create(GitHubService.class); Call<List<Contributor>> call = gitHubService.contributrors("square", "retrofit"); try { List<Contributor> body = call.execute().body(); for (Contributor contributor : body) { Log.i(TAG, contributor.login + " " + contributor.id); } } catch (IOException e) { e.printStackTrace(); } } }.start(); }}
0 0
- retrofit与rx_java结合使用基础篇之一
- Rx_Java基础篇之一
- Retrofit与RxAndroid结合使用
- Retrofit与Rxjava结合使用
- RxJava 与 Retrofit 结合使用,史上最牛搭档
- Retrofit 与Rxjava完美结合使用
- Retrofit与Rxjava结合
- RxJava + Retrofit 结合使用
- Retrofit结合rxjava使用
- RX_JAVA
- Rxjava,retrofit,Rxjava结合使用与代码封装
- 使用Retrofit结合RxJava实现Get与Post网络请求
- RxJava与Retrofit结合学习
- Retrofit 和 RxJava 结合使用
- RxJava,Retrofit,OkHttp3结合使用
- Retrofit和RxJava结合使用
- RxJava和Retrofit结合使用
- RxJava和Retrofit结合使用
- prototype和__proto__
- HTTP和HTTPS详解
- Hadoop-MapReduce的工作机制
- 一些微信第三方登录需要注意的地方
- memcache缓存雪崩现象
- retrofit与rx_java结合使用基础篇之一
- File操作-InputStreamReader/Writer
- 火狐安装后双击没反应的解决办法
- 423. Reconstruct Original Digits from English
- 关于log4j root logger 标签 以及additivity 属性
- CTP JAVA_API(JCTP)编译(利用Swig封装C++动态库)linux版64位
- 自定义悬浮头部标题栏HeaderFloatTitle(支持背景/透明度/位移的变化)
- Centos7 安装 tomcat9 后访问慢且 shutdown.sh 报错,提示 8005 端口未启用
- Android - 判断Android设备是手机还是平板?