【Android高级】RxJava2.0和Retrofit2.0的使用探究
来源:互联网 发布:整顿网络 英语 编辑:程序博客网 时间:2024/05/21 20:25
初次测试:
MainAct:
package com.rx.test.rxtestproject;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import com.lidroid.xutils.ViewUtils;import com.lidroid.xutils.view.annotation.ViewInject;import com.lidroid.xutils.view.annotation.event.OnClick;import com.myrest.test.IGitApi;import com.myrest.test.UserBean;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.Arrays;import java.util.List;import retrofit.Call;import retrofit.Callback;import retrofit.GsonConverterFactory;import retrofit.Response;import retrofit.Retrofit;import rx.Observable;import rx.Subscriber;import rx.android.schedulers.AndroidSchedulers;import rx.functions.Action1;import rx.functions.Func1;import rx.schedulers.Schedulers;public class MainActivity extends AppCompatActivity { @ViewInject(R.id.textView) TextView textView; @ViewInject(R.id.imageView) ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewUtils.inject(this); } public void test1(View v) { Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { subscriber.onNext("haha"); } }); Subscriber<String> subscriber = new Subscriber<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(String s) { textView.setText(s); } }; observable.subscribe(subscriber); } public void test2(View v) {// Observable<String>observable=Observable.just("just test");// Action1<String>action1=new Action1<String>() {// @Override// public void call(String s) {// textView.setText(s);// }// };//// observable.subscribe(action1);// Observable.just("Test just").subscribe(new Action1<String>() {// @Override// public void call(String s) {// textView.setText(s);// }// });// Observable.just("test map")// .map(new Func1<String, String >() {// @Override// public String call(String s) {// return s+" in call";// }// }).map(new Func1<String, String>() {// @Override// public String call(String s) {// return s+" a in";// }// }).subscribe(new Action1<String>() {// @Override// public void call(String s) {// textView.setText(s);// }// }); Observable.just(5).map(new Func1<Integer, String>() { @Override public String call(Integer integer) { return integer + " s1"; } }).map(new Func1<String, Integer>() { @Override public Integer call(String s) { return 9; } }).subscribe(new Action1<Integer>() { @Override public void call(Integer integer) { textView.setText("last:" + integer); } }); } public void test3(View v) { String[] strs = {"a", "b", "c"};// Observable.from(strs).subscribe(new Subscriber<String>() {// @Override// public void onStart() {// // super.onStart();// textView.setText("");// Log.i("tag", "onStart");// }//// @Override// public void onCompleted() {// Log.i("tag", "onCompleted");// }//// @Override// public void onError(Throwable e) {//// }//// @Override// public void onNext(String s) {// Log.i("tag", "onNext");// textView.append(s);// }// }); Observable.just(Arrays.asList(strs)).flatMap(new Func1<List<String>, Observable<String>>() { @Override public Observable<String> call(List<String> strings) { return Observable.from(strings); } }).subscribe(new Subscriber<String>() { @Override public void onStart() { textView.setText(""); Log.i("tag", "onStart"); } @Override public void onCompleted() { Log.i("tag", "onCompleted"); } @Override public void onError(Throwable e) { } @Override public void onNext(String s) { Log.i("tag", "onNext"); textView.append(s); } }); } public void test4(View v) { Observable.create(new Observable.OnSubscribe<Bitmap>() { @Override public void call(Subscriber<? super Bitmap> subscriber) { try { URL url = new URL("http://a.hiphotos.baidu.com/zhidao/pic/item/0824ab18972bd4073df731c379899e510fb309df.jpg"); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); InputStream inputStream = httpURLConnection.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(inputStream); subscriber.onNext(bitmap); Log.i("tag", "http:" + Thread.currentThread().getName()); } catch (Exception e) { e.printStackTrace(); } } }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Bitmap>() { @Override public void call(Bitmap bitmap) { imageView.setImageBitmap(bitmap); Log.i("tag", "setbitmap:" + Thread.currentThread().getName()); } });// .subscribeOn(Schedulers.io()//// ).//// observeOn(AndroidSchedulers.mainThread()//// ).//// subscribe(new Action1<Bitmap>() {// @Override// public void call (Bitmap bitmap){//// }// }//// );// } } public void test5(View v) { String endUrl = "http://ip.taobao.com"; Retrofit retrofit = new Retrofit.Builder().baseUrl(endUrl).addConverterFactory(GsonConverterFactory.create()).build(); ApiService apiService = retrofit.create(ApiService.class); Call<GetIpInfoResponse> call = apiService.getIpInfo("63.223.108.42"); call.enqueue(new Callback<GetIpInfoResponse>() { @Override public void onResponse(Response<GetIpInfoResponse> response, Retrofit retrofit) { GetIpInfoResponse getIpInfoResponse = response.body(); textView.setText(getIpInfoResponse.data.country + "\n" + getIpInfoResponse.toString()); } @Override public void onFailure(Throwable t) { textView.setText("fail"); } }); } @OnClick(R.id.button) public void test6(View v) {// String API = "http://api.github.com";//// Retrofit retrofit = new Retrofit.Builder().baseUrl(API).addConverterFactory(GsonConverterFactory.create()).build();////// IGitApi iGitApi = retrofit.create(IGitApi.class);//// iGitApi.getData("scboyhj", new Callback<UserBean>() {//// @Override// public void onResponse(Response<UserBean> response, Retrofit retrofit) {// UserBean userBean = response.body();// textView.setText(userBean.toString());// }//// @Override// public void onFailure(Throwable t) {// textView.setText("fail in");// }// }); String endUrl = "https://api.github.com"; Retrofit retrofit = new Retrofit.Builder().baseUrl(endUrl).addConverterFactory(GsonConverterFactory.create()).build(); IGitApi apiService = retrofit.create(IGitApi.class); Call<UserBean> call = apiService.getData("scboyhj"); call.enqueue(new Callback<UserBean>() { @Override public void onResponse(Response<UserBean> response, Retrofit retrofit) { UserBean userBean = response.body(); textView.setText(userBean.toString()); } @Override public void onFailure(Throwable t) { textView.setText("fail in"); } }); }}
接口
public interface IGitApi { @GET("/users/{user}") public Call<UserBean> getData(@Path("user") String user);}
综合测试:分开和一起
public class MainAct extends AppCompatActivity { @ViewInject(R.id.retrorxIg) ImageView imageView; @ViewInject(R.id.retrorxTv) TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.rxretrolay); ViewUtils.inject(this); } @OnClick(R.id.retroBt) public void testRe(View v) { String url = "https://api.github.com"; Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(url).build(); GitInter gitInter = retrofit.create(GitInter.class); Call<GitBean> gitBeanCall = gitInter.getInfo("scboyhj"); gitBeanCall.enqueue(new Callback<GitBean>() { @Override public void onResponse(Response<GitBean> response, Retrofit retrofit) { GitBean gitBean = response.body(); textView.setText(gitBean.toString()); } @Override public void onFailure(Throwable t) { textView.setText("on fail"); } }); } @OnClick(R.id.rxBt) public void testRx(View v) { Observable.just("").map(new Func1<String, Bitmap>() { @Override public Bitmap call(String s) { try { URL url = new URL("http://pic30.nipic.com/20130618/11860366_201437262000_2.jpg"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); Bitmap bitmap = BitmapFactory.decodeStream(urlConnection.getInputStream()); return bitmap; } catch (MalformedURLException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Bitmap>() { @Override public void call(Bitmap bitmap) { if (bitmap == null) { textView.setText("internet fail"); } else { imageView.setImageBitmap(bitmap); } } }); } @OnClick(R.id.rx_retro_Bt) public void testRx_Retro(View v){ String url = "https://api.github.com"; Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).baseUrl(url).build(); final GitInter2 gitInter = retrofit.create(GitInter2.class); Observable.just("scboyhj").flatMap(new Func1<String, Observable<GitBean>>() { @Override public Observable<GitBean> call(String s) { Observable<GitBean> gitBeanCall = gitInter.getInfo(s); return gitBeanCall; } }).subscribeOn(Schedulers.io()). observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<GitBean>() { @Override public void onCompleted() { Log.i("tag","onCompleted"); } @Override public void onError(Throwable e) { Log.i("tag","onError"); } @Override public void onNext(GitBean gitBean) { textView.setText(gitBean.toString()); } }); }}
0 1
- 【Android高级】RxJava2.0和Retrofit2.0的使用探究
- RxJava2和Retrofit2.0的简单使用
- Android使用Retrofit2.0和RxJava2.0处理网络请求
- Android 优雅的让RxJava2.0+Retrofit2.0结合使用
- Android 优雅的让RxJava2.0+Retrofit2.0结合使用
- android Retrofit2.0 RxJava2.0
- Retrofit2和RxJava2的使用介绍一
- Rxjava2.0和Retrofit2.0结合使用(2)post请求
- Android基于Retrofit2.0和RXJava2.0 的HTTPS网络请求搭建
- Retrofit2.0+Rxjava2 相关的文章
- Retrofit2.0+Rxjava2.0+MVP使用小记(一)
- 继续趁热!!!Retrofit2使用(基于RxJava2.0,修正一次)
- Android详细的登录注册功能MVP+Rxjava2.0+Retrofit2.0相结合的实战篇章
- Rxjava2和Retrofit2网络库的封装
- Retrofit2.0与RxJava2.0结合出现的问题解决
- Android RxJava2.0的简单使用
- RxJava2.0的使用
- Retrofit2.0与RXJava2.0最新结合:
- MVP
- ConcurrentHashMap的锁分段技术
- YTU 2574: 空白格式化
- Project Perfect让Swift在服务器端跑起来-Hi Linux(二)
- mvc
- 【Android高级】RxJava2.0和Retrofit2.0的使用探究
- 帆软报表学习—单元格扩展和父子格
- YTU 2579: 填空题----删除指定字符
- 《machine learning with spark》学习笔记--聚类
- domino uploadify不刷新页面上传多附件
- 基于《如何阅读一本书完整》提炼出分析阅读的核心框架
- 常见的文件后缀名和对应的编辑工具(下)
- Win7系统IIS7+Tomcat7集成
- [今日所学] 第二天