Skip to content

Commit f7c38d9

Browse files
author
Kaushik Gopal
committed
fix: move to independent package + remove dead code + add readme
1 parent ddcbefc commit f7c38d9

File tree

7 files changed

+112
-169
lines changed

7 files changed

+112
-169
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ This is the debounce/throttleWithTimeout method in RxJava.
4141

4242
Since it was a presentation, Jake only put up the most important code snippets in [his slides](https://speakerdeck.com/jakewharton/2014-1). Also he uses Java 8 in them, so I flushed those examples out in ~~good~~ old Java 6. (Note: you're most likely to hit the GitHub API quota pretty fast so send in an OAuth-token as a parameter if you want to keep running these examples often).
4343

44+
### Volley Demo
45+
46+
[Volley](http://developer.android.com/training/volley/index.html) is another networking library introduced by [Google at IO '13](https://www.youtube.com/watch?v=yhv8l9F44qo). A kind citizen of github contributed this example so we know how to integrate Volley with RxJava.
47+
48+
4449
### Orchestrating Observables. Make parallel network calls, then combine the result into a single data point (flatmap + zip)
4550

4651
The below ascii diagram expresses the intention of our next example with panache. f1,f2,3,f4,f5 are essentially network calls that when made, give back a result that's needed for a future calculation.

app/build.gradle

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@ apply plugin: 'com.android.application'
33
dependencies {
44
compile 'com.android.support:support-v13:23.0.1'
55

6+
67
compile 'io.reactivex:rxandroid:1.0.1'
8+
compile 'io.reactivex:rxjava-math:1.0.0'
9+
compile 'io.reactivex:rxjava:1.0.14'
710
// Because RxAndroid releases are few and far between, it is recommended you also
811
// explicitly depend on RxJava's latest version for bug fixes and new features.
9-
compile 'io.reactivex:rxjava:1.0.14'
10-
compile 'io.reactivex:rxjava-math:1.0.0'
1112
compile 'com.jakewharton.rxbinding:rxbinding:0.2.0'
1213

13-
compile 'com.jakewharton:butterknife:7.0.1'
1414
compile 'com.jakewharton.timber:timber:2.4.2'
15-
compile 'com.squareup.retrofit:retrofit:1.6.1'
16-
compile 'com.squareup.okhttp:okhttp:2.0.0'
15+
compile 'com.jakewharton:butterknife:7.0.1'
16+
compile 'com.mcxiaoke.volley:library:1.0.19'
1717
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
18+
compile 'com.squareup.okhttp:okhttp:2.0.0'
19+
compile 'com.squareup.retrofit:retrofit:1.6.1'
1820

1921
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'
2022
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'
21-
compile 'com.mcxiaoke.volley:library:1.0.19'
22-
2323
}
2424

2525
android {
2626
compileSdkVersion 23
27-
buildToolsVersion '23.0.1'
27+
buildToolsVersion '23.0.2'
2828

2929
defaultConfig {
3030
applicationId "com.morihacky.android.rxjava"

app/src/main/java/com/morihacky/android/rxjava/MyApp.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import android.app.Application;
44

5-
import com.morihacky.android.rxjava.wiring.MyVolley;
5+
import com.morihacky.android.rxjava.volley.MyVolley;
66
import com.squareup.leakcanary.LeakCanary;
77
import com.squareup.leakcanary.RefWatcher;
88
import timber.log.Timber;
@@ -28,9 +28,9 @@ public void onCreate() {
2828
_instance = (MyApp) getApplicationContext();
2929
_refWatcher = LeakCanary.install(this);
3030

31-
Timber.plant(new Timber.DebugTree());
32-
33-
//volley init
31+
// Initialize Volley
3432
MyVolley.init(this);
33+
34+
Timber.plant(new Timber.DebugTree());
3535
}
3636
}

app/src/main/java/com/morihacky/android/rxjava/fragments/MainFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import butterknife.ButterKnife;
1515
import butterknife.OnClick;
16+
import com.morihacky.android.rxjava.volley.VolleyDemoFragment;
1617

1718
public class MainFragment
1819
extends BaseFragment {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.morihacky.android.rxjava.volley;
2+
3+
import android.content.Context;
4+
import com.android.volley.RequestQueue;
5+
import com.android.volley.toolbox.Volley;
6+
7+
/**
8+
* Helper class that is used to provide references to initialized RequestQueue(s) and ImageLoader(s)
9+
*
10+
* @author Ognyan Bankov
11+
*/
12+
public class MyVolley {
13+
private static RequestQueue mRequestQueue;
14+
15+
private MyVolley() {
16+
// no instances
17+
}
18+
19+
public static void init(Context context) {
20+
mRequestQueue = Volley.newRequestQueue(context);
21+
}
22+
23+
public static RequestQueue getRequestQueue() {
24+
if (mRequestQueue != null) {
25+
return mRequestQueue;
26+
} else {
27+
throw new IllegalStateException("RequestQueue not initialized");
28+
}
29+
}
30+
}

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

Lines changed: 64 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.morihacky.android.rxjava.fragments;
1+
package com.morihacky.android.rxjava.volley;
22

33
import android.os.Bundle;
44
import android.os.Handler;
@@ -8,27 +8,22 @@
88
import android.view.LayoutInflater;
99
import android.view.View;
1010
import android.view.ViewGroup;
11-
import android.widget.Button;
1211
import android.widget.ListView;
13-
12+
import butterknife.Bind;
13+
import butterknife.ButterKnife;
14+
import butterknife.OnClick;
1415
import com.android.volley.Request;
1516
import com.android.volley.VolleyError;
1617
import com.android.volley.toolbox.JsonObjectRequest;
1718
import com.android.volley.toolbox.RequestFuture;
1819
import com.morihacky.android.rxjava.R;
20+
import com.morihacky.android.rxjava.fragments.BaseFragment;
1921
import com.morihacky.android.rxjava.wiring.LogAdapter;
20-
import com.morihacky.android.rxjava.wiring.MyVolley;
21-
22-
import org.json.JSONObject;
23-
2422
import java.nio.charset.Charset;
2523
import java.util.ArrayList;
2624
import java.util.List;
2725
import java.util.concurrent.ExecutionException;
28-
29-
import butterknife.Bind;
30-
import butterknife.ButterKnife;
31-
import butterknife.OnClick;
26+
import org.json.JSONObject;
3227
import rx.Observable;
3328
import rx.Observer;
3429
import rx.android.schedulers.AndroidSchedulers;
@@ -37,43 +32,25 @@
3732
import rx.subscriptions.CompositeSubscription;
3833
import timber.log.Timber;
3934

40-
/**
41-
* Created by zhangxitao on 15/12/30.
42-
*/
43-
4435
public class VolleyDemoFragment
45-
extends BaseFragment {
36+
extends BaseFragment {
37+
4638
public static final String TAG = "VolleyDemoFragment";
4739

48-
@Bind(R.id.list_threading_log)
49-
ListView _logsList;
50-
@Bind(R.id.btn_start_operation)
51-
Button _tapBtn;
40+
@Bind(R.id.list_threading_log) ListView _logsList;
5241

53-
private LogAdapter _adapter;
5442
private List<String> _logs;
43+
private LogAdapter _adapter;
5544

56-
private CompositeSubscription mCompositeSubscription = new CompositeSubscription();
57-
58-
@OnClick(R.id.btn_start_operation)
59-
void startRequest() {
60-
startVolleyRequest();
61-
}
62-
63-
64-
@Override
65-
public void onStart() {
66-
super.onStart();
67-
68-
/**
69-
* @condition: RxJava future request with volley
70-
*/
71-
}
45+
private CompositeSubscription _compositeSubscription = new CompositeSubscription();
7246

7347
@Override
74-
public void onPause() {
75-
super.onPause();
76-
mCompositeSubscription.unsubscribe();
48+
public View onCreateView(LayoutInflater inflater,
49+
@Nullable ViewGroup container,
50+
@Nullable Bundle savedInstanceState) {
51+
View layout = inflater.inflate(R.layout.fragment_volley, container, false);
52+
ButterKnife.bind(this, layout);
53+
return layout;
7754
}
7855

7956
@Override
@@ -83,66 +60,15 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
8360
}
8461

8562
@Override
86-
public View onCreateView(LayoutInflater inflater,
87-
@Nullable ViewGroup container,
88-
@Nullable Bundle savedInstanceState) {
89-
View layout = inflater.inflate(R.layout.fragment_volley, container, false);
90-
ButterKnife.bind(this, layout);
91-
return layout;
92-
}
93-
94-
private void startVolleyRequest() {
95-
mCompositeSubscription.add(newGetRouteData()
96-
.subscribeOn(Schedulers.io())
97-
.observeOn(AndroidSchedulers.mainThread())
98-
.subscribe(new Observer<JSONObject>() {
99-
@Override
100-
public void onCompleted() {
101-
Log.e(TAG, "onCompleted");
102-
Timber.d("----- onCompleted");
103-
_log("onCompleted ");
104-
}
105-
106-
@Override
107-
public void onError(Throwable e) {
108-
VolleyError cause = (VolleyError) e.getCause();
109-
String s = new String(cause.networkResponse.data, Charset.forName("UTF-8"));
110-
Log.e(TAG, s);
111-
Log.e(TAG, cause.toString());
112-
_log("onError " + s);
113-
114-
}
115-
116-
@Override
117-
public void onNext(JSONObject jsonObject) {
118-
Log.e(TAG, "onNext " + jsonObject.toString());
119-
_log("onNext " + jsonObject.toString());
120-
121-
}
122-
}));
123-
}
124-
125-
126-
/**
127-
* @use handle response from future request, in my case JsonObject.
128-
*/
129-
private JSONObject getRouteData() throws ExecutionException, InterruptedException {
130-
RequestFuture<JSONObject> future = RequestFuture.newFuture();
131-
String url = "http://www.weather.com.cn/adat/sk/101010100.html";
132-
final Request.Priority priority = Request.Priority.IMMEDIATE;
133-
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, future, future);
134-
MyVolley.getRequestQueue().add(req);
135-
return future.get();
63+
public void onPause() {
64+
super.onPause();
65+
_compositeSubscription.unsubscribe();
13666
}
13767

138-
/**
139-
* @use the observable, same type data Jsob Object
140-
*/
14168
public Observable<JSONObject> newGetRouteData() {
14269
return Observable.defer(new Func0<Observable<JSONObject>>() {
14370
@Override
14471
public Observable<JSONObject> call() {
145-
Exception exception;
14672
try {
14773
return Observable.just(getRouteData());
14874
} catch (InterruptedException | ExecutionException e) {
@@ -153,6 +79,50 @@ public Observable<JSONObject> call() {
15379
});
15480
}
15581

82+
@OnClick(R.id.btn_start_operation)
83+
void startRequest() {
84+
startVolleyRequest();
85+
}
86+
87+
private void startVolleyRequest() {
88+
_compositeSubscription.add(newGetRouteData().subscribeOn(Schedulers.io())
89+
.observeOn(AndroidSchedulers.mainThread())
90+
.subscribe(new Observer<JSONObject>() {
91+
@Override
92+
public void onCompleted() {
93+
Log.e(TAG, "onCompleted");
94+
Timber.d("----- onCompleted");
95+
_log("onCompleted ");
96+
}
97+
98+
@Override
99+
public void onError(Throwable e) {
100+
VolleyError cause = (VolleyError) e.getCause();
101+
String s = new String(cause.networkResponse.data, Charset.forName("UTF-8"));
102+
Log.e(TAG, s);
103+
Log.e(TAG, cause.toString());
104+
_log("onError " + s);
105+
106+
}
107+
108+
@Override
109+
public void onNext(JSONObject jsonObject) {
110+
Log.e(TAG, "onNext " + jsonObject.toString());
111+
_log("onNext " + jsonObject.toString());
112+
113+
}
114+
}));
115+
}
116+
117+
private JSONObject getRouteData() throws ExecutionException, InterruptedException {
118+
RequestFuture<JSONObject> future = RequestFuture.newFuture();
119+
String url = "http://www.weather.com.cn/adat/sk/101010100.html";
120+
final Request.Priority priority = Request.Priority.IMMEDIATE;
121+
JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, url, future, future);
122+
MyVolley.getRequestQueue().add(req);
123+
return future.get();
124+
}
125+
156126
// -----------------------------------------------------------------------------------
157127
// Methods that help wiring up the example (irrelevant to RxJava)
158128

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

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)