|
9 | 9 | import android.view.View; |
10 | 10 | import android.view.ViewGroup; |
11 | 11 | import android.widget.ListView; |
12 | | - |
| 12 | +import butterknife.Bind; |
| 13 | +import butterknife.ButterKnife; |
| 14 | +import butterknife.OnClick; |
13 | 15 | import com.android.volley.Request; |
14 | 16 | import com.android.volley.VolleyError; |
15 | 17 | import com.android.volley.toolbox.JsonObjectRequest; |
16 | 18 | import com.android.volley.toolbox.RequestFuture; |
17 | 19 | import com.morihacky.android.rxjava.R; |
18 | 20 | import com.morihacky.android.rxjava.fragments.BaseFragment; |
19 | 21 | import com.morihacky.android.rxjava.wiring.LogAdapter; |
20 | | - |
21 | | -import org.json.JSONObject; |
22 | | - |
| 22 | +import io.reactivex.Flowable; |
| 23 | +import io.reactivex.android.schedulers.AndroidSchedulers; |
| 24 | +import io.reactivex.disposables.CompositeDisposable; |
| 25 | +import io.reactivex.schedulers.Schedulers; |
| 26 | +import io.reactivex.subscribers.DisposableSubscriber; |
23 | 27 | import java.nio.charset.Charset; |
24 | 28 | import java.util.ArrayList; |
25 | 29 | import java.util.List; |
26 | 30 | import java.util.concurrent.ExecutionException; |
27 | | - |
28 | | -import butterknife.Bind; |
29 | | -import butterknife.ButterKnife; |
30 | | -import butterknife.OnClick; |
31 | | -import rx.Observable; |
32 | | -import rx.Observer; |
33 | | -import rx.android.schedulers.AndroidSchedulers; |
34 | | -import rx.schedulers.Schedulers; |
35 | | -import rx.subscriptions.CompositeSubscription; |
| 31 | +import org.json.JSONObject; |
36 | 32 | import timber.log.Timber; |
37 | 33 |
|
38 | 34 | public class VolleyDemoFragment |
@@ -45,7 +41,7 @@ public class VolleyDemoFragment |
45 | 41 | private List<String> _logs; |
46 | 42 | private LogAdapter _adapter; |
47 | 43 |
|
48 | | - private CompositeSubscription _compositeSubscription = new CompositeSubscription(); |
| 44 | + private CompositeDisposable _disposables = new CompositeDisposable(); |
49 | 45 |
|
50 | 46 | @Override |
51 | 47 | public View onCreateView(LayoutInflater inflater, |
@@ -65,7 +61,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
65 | 61 | @Override |
66 | 62 | public void onPause() { |
67 | 63 | super.onPause(); |
68 | | - _compositeSubscription.clear(); |
| 64 | + _disposables.clear(); |
69 | 65 | } |
70 | 66 |
|
71 | 67 | @Override |
@@ -75,18 +71,18 @@ public void onDestroyView() { |
75 | 71 | } |
76 | 72 |
|
77 | 73 | /** |
78 | | - * Creates and returns an observable generated from the Future returned from |
| 74 | + * Creates and returns an observable generated from the Future returned from |
79 | 75 | * {@code getRouteData()}. The observable can then be subscribed to as shown in |
80 | 76 | * {@code startVolleyRequest()} |
81 | 77 | * @return Observable<JSONObject> |
82 | 78 | */ |
83 | | - public Observable<JSONObject> newGetRouteData() { |
84 | | - return Observable.defer(() -> { |
| 79 | + public Flowable<JSONObject> newGetRouteData() { |
| 80 | + return Flowable.defer(() -> { |
85 | 81 | try { |
86 | | - return Observable.just(getRouteData()); |
| 82 | + return Flowable.just(getRouteData()); |
87 | 83 | } catch (InterruptedException | ExecutionException e) { |
88 | 84 | Log.e("routes", e.getMessage()); |
89 | | - return Observable.error(e); |
| 85 | + return Flowable.error(e); |
90 | 86 | } |
91 | 87 | }); |
92 | 88 | } |
@@ -97,38 +93,44 @@ void startRequest() { |
97 | 93 | } |
98 | 94 |
|
99 | 95 | private void startVolleyRequest() { |
100 | | - _compositeSubscription.add(newGetRouteData().subscribeOn(Schedulers.io()) |
| 96 | + DisposableSubscriber<JSONObject> d = new DisposableSubscriber<JSONObject>() { |
| 97 | + @Override |
| 98 | + public void onNext(JSONObject jsonObject) { |
| 99 | + Log.e(TAG, "onNext " + jsonObject.toString()); |
| 100 | + _log("onNext " + jsonObject.toString()); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onError(Throwable e) { |
| 106 | + VolleyError cause = (VolleyError) e.getCause(); |
| 107 | + String s = new String(cause.networkResponse.data, Charset.forName("UTF-8")); |
| 108 | + Log.e(TAG, s); |
| 109 | + Log.e(TAG, cause.toString()); |
| 110 | + _log("onError " + s); |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public void onComplete() { |
| 116 | + Log.e(TAG, "onCompleted"); |
| 117 | + Timber.d("----- onCompleted"); |
| 118 | + _log("onCompleted "); |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + newGetRouteData() |
| 123 | + .subscribeOn(Schedulers.io()) |
101 | 124 | .observeOn(AndroidSchedulers.mainThread()) |
102 | | - .subscribe(new Observer<JSONObject>() { |
103 | | - @Override |
104 | | - public void onCompleted() { |
105 | | - Log.e(TAG, "onCompleted"); |
106 | | - Timber.d("----- onCompleted"); |
107 | | - _log("onCompleted "); |
108 | | - } |
109 | | - |
110 | | - @Override |
111 | | - public void onError(Throwable e) { |
112 | | - VolleyError cause = (VolleyError) e.getCause(); |
113 | | - String s = new String(cause.networkResponse.data, Charset.forName("UTF-8")); |
114 | | - Log.e(TAG, s); |
115 | | - Log.e(TAG, cause.toString()); |
116 | | - _log("onError " + s); |
117 | | - |
118 | | - } |
119 | | - |
120 | | - @Override |
121 | | - public void onNext(JSONObject jsonObject) { |
122 | | - Log.e(TAG, "onNext " + jsonObject.toString()); |
123 | | - _log("onNext " + jsonObject.toString()); |
124 | | - |
125 | | - } |
126 | | - })); |
| 125 | + .subscribe(d); |
| 126 | + |
| 127 | + _disposables.add(d); |
127 | 128 | } |
| 129 | + |
128 | 130 | /** |
129 | 131 | * Converts the Asynchronous Request into a Synchronous Future that can be used to |
130 | 132 | * block via {@code Future.get()}. Observables require blocking/synchronous functions |
131 | | - * @return JSONObject |
| 133 | + * @return JSONObject |
132 | 134 | * @throws ExecutionException |
133 | 135 | * @throws InterruptedException |
134 | 136 | */ |
|