Skip to content

Commit 23f1fbf

Browse files
author
Kaushik Gopal
committed
feat: port volley example
port example Networking with Volley
1 parent 897ea73 commit 23f1fbf

File tree

2 files changed

+50
-50
lines changed

2 files changed

+50
-50
lines changed

app/src/main/java/com/morihacky/android/rxjava/volley/MyVolley.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
/**
88
* Helper class that is used to provide references to initialized RequestQueue(s) and ImageLoader(s)
9-
*
10-
* @author Ognyan Bankov
119
*/
1210
public class MyVolley {
1311
private static RequestQueue mRequestQueue;
@@ -20,7 +18,7 @@ public static void init(Context context) {
2018
mRequestQueue = Volley.newRequestQueue(context);
2119
}
2220

23-
public static RequestQueue getRequestQueue() {
21+
static RequestQueue getRequestQueue() {
2422
if (mRequestQueue != null) {
2523
return mRequestQueue;
2624
} else {

app/src/main/java/com/morihacky/android/rxjava/volley/VolleyDemoFragment.java

+49-47
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,26 @@
99
import android.view.View;
1010
import android.view.ViewGroup;
1111
import android.widget.ListView;
12-
12+
import butterknife.Bind;
13+
import butterknife.ButterKnife;
14+
import butterknife.OnClick;
1315
import com.android.volley.Request;
1416
import com.android.volley.VolleyError;
1517
import com.android.volley.toolbox.JsonObjectRequest;
1618
import com.android.volley.toolbox.RequestFuture;
1719
import com.morihacky.android.rxjava.R;
1820
import com.morihacky.android.rxjava.fragments.BaseFragment;
1921
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;
2327
import java.nio.charset.Charset;
2428
import java.util.ArrayList;
2529
import java.util.List;
2630
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;
3632
import timber.log.Timber;
3733

3834
public class VolleyDemoFragment
@@ -45,7 +41,7 @@ public class VolleyDemoFragment
4541
private List<String> _logs;
4642
private LogAdapter _adapter;
4743

48-
private CompositeSubscription _compositeSubscription = new CompositeSubscription();
44+
private CompositeDisposable _disposables = new CompositeDisposable();
4945

5046
@Override
5147
public View onCreateView(LayoutInflater inflater,
@@ -65,7 +61,7 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
6561
@Override
6662
public void onPause() {
6763
super.onPause();
68-
_compositeSubscription.clear();
64+
_disposables.clear();
6965
}
7066

7167
@Override
@@ -75,18 +71,18 @@ public void onDestroyView() {
7571
}
7672

7773
/**
78-
* Creates and returns an observable generated from the Future returned from
74+
* Creates and returns an observable generated from the Future returned from
7975
* {@code getRouteData()}. The observable can then be subscribed to as shown in
8076
* {@code startVolleyRequest()}
8177
* @return Observable<JSONObject>
8278
*/
83-
public Observable<JSONObject> newGetRouteData() {
84-
return Observable.defer(() -> {
79+
public Flowable<JSONObject> newGetRouteData() {
80+
return Flowable.defer(() -> {
8581
try {
86-
return Observable.just(getRouteData());
82+
return Flowable.just(getRouteData());
8783
} catch (InterruptedException | ExecutionException e) {
8884
Log.e("routes", e.getMessage());
89-
return Observable.error(e);
85+
return Flowable.error(e);
9086
}
9187
});
9288
}
@@ -97,38 +93,44 @@ void startRequest() {
9793
}
9894

9995
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())
101124
.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);
127128
}
129+
128130
/**
129131
* Converts the Asynchronous Request into a Synchronous Future that can be used to
130132
* block via {@code Future.get()}. Observables require blocking/synchronous functions
131-
* @return JSONObject
133+
* @return JSONObject
132134
* @throws ExecutionException
133135
* @throws InterruptedException
134136
*/

0 commit comments

Comments
 (0)