初次测试:
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;@Overrideprotected 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>() {@Overridepublic void call(Subscriber<? super String> subscriber) {subscriber.onNext("haha");}});Subscriber<String> subscriber = new Subscriber<String>() {@Overridepublic void onCompleted() {}@Overridepublic void onError(Throwable e) {}@Overridepublic 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>() {@Overridepublic String call(Integer integer) {return integer + " s1";}}).map(new Func1<String, Integer>() {@Overridepublic Integer call(String s) {return 9;}}).subscribe(new Action1<Integer>() {@Overridepublic 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>>() {@Overridepublic Observable<String> call(List<String> strings) {return Observable.from(strings);}}).subscribe(new Subscriber<String>() {@Overridepublic void onStart() {textView.setText("");Log.i("tag", "onStart");}@Overridepublic void onCompleted() {Log.i("tag", "onCompleted");}@Overridepublic void onError(Throwable e) {}@Overridepublic void onNext(String s) {Log.i("tag", "onNext");textView.append(s);}});}public void test4(View v) {Observable.create(new Observable.OnSubscribe<Bitmap>() {@Overridepublic 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>() {@Overridepublic 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>() {@Overridepublic void onResponse(Response<GetIpInfoResponse> response, Retrofit retrofit) {GetIpInfoResponse getIpInfoResponse = response.body();textView.setText(getIpInfoResponse.data.country + "\n" + getIpInfoResponse.toString());}@Overridepublic 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>() {@Overridepublic void onResponse(Response<UserBean> response, Retrofit retrofit) {UserBean userBean = response.body();textView.setText(userBean.toString());}@Overridepublic 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;@Overrideprotected 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>() {@Overridepublic void onResponse(Response<GitBean> response, Retrofit retrofit) {GitBean gitBean = response.body();textView.setText(gitBean.toString());}@Overridepublic void onFailure(Throwable t) {textView.setText("on fail");}});}@OnClick(R.id.rxBt)public void testRx(View v) {Observable.just("").map(new Func1<String, Bitmap>() {@Overridepublic 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>() {@Overridepublic 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>>() {@Overridepublic Observable<GitBean> call(String s) {Observable<GitBean> gitBeanCall = gitInter.getInfo(s);return gitBeanCall;}}).subscribeOn(Schedulers.io()). observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<GitBean>() {@Overridepublic void onCompleted() {Log.i("tag","onCompleted");}@Overridepublic void onError(Throwable e) {Log.i("tag","onError");}@Overridepublic void onNext(GitBean gitBean) {textView.setText(gitBean.toString());}});}}