Skip to content

Commit cb051e0

Browse files
committed
add Retrofit and RxAndroid examples
1 parent 66f8ede commit cb051e0

18 files changed

Lines changed: 742 additions & 35 deletions

app/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ dependencies {
3636
compile 'io.reactivex:rxjava:1.0.14'
3737
compile 'io.reactivex:rxjava-math:1.0.0'
3838
compile 'com.jakewharton.rxbinding:rxbinding:0.2.0'
39+
/*rotrofit*/
40+
compile 'com.squareup.retrofit2:retrofit:+'
41+
compile 'com.squareup.retrofit2:converter-gson:+'
42+
compile 'com.squareup.retrofit2:adapter-rxjava:+'
43+
compile 'com.google.code.gson:gson:+'
44+
45+
compile 'io.reactivex:rxandroid:1.2.1'
3946
}

app/src/main/AndroidManifest.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.demo.maat.hello_rxjava">
4-
4+
<uses-permission android:name="android.permission.INTERNET"/>
5+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
56
<application
67
android:allowBackup="true"
78
android:icon="@mipmap/ic_launcher"
@@ -37,7 +38,15 @@
3738
android:launchMode="singleTop"/>
3839
<activity
3940
android:name=".DebounceEditTextActivity"
40-
android:label="Polling"
41+
android:label="Debounce"
42+
android:launchMode="singleTop"/>
43+
<activity
44+
android:name=".RxJavaRetrofitActivity"
45+
android:label="RxJavaRetrofit"
46+
android:launchMode="singleTop"/>
47+
<activity
48+
android:name=".RxAndroidActivity"
49+
android:label="RxJavaRetrofit"
4150
android:launchMode="singleTop"/>
4251
</application>
4352

app/src/main/java/com/demo/maat/hello_rxjava/MainActivity.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.os.Bundle;
55
import android.support.annotation.Nullable;
66
import android.support.v7.app.AppCompatActivity;
7-
import android.util.Log;
87
import android.view.View;
98
import android.widget.Button;
109

@@ -19,6 +18,8 @@ public class MainActivity extends AppCompatActivity {
1918
Button mBtnRxjava;
2019
@BindView(R.id.btn_retrofit)
2120
Button mBtnRetrofit;
21+
@BindView(R.id.btn_rxandroid)
22+
Button mBtnRxAndroid;
2223

2324

2425
@Override
@@ -30,15 +31,20 @@ protected void onCreate(Bundle savedInstanceState) {
3031
}
3132

3233

33-
@OnClick({R.id.btn_rxjava, R.id.btn_retrofit})
34+
@OnClick({R.id.btn_rxjava, R.id.btn_retrofit,R.id.btn_rxandroid})
3435
public void onClick(View view) {
3536
switch (view.getId()) {
3637
case R.id.btn_rxjava:
37-
Log.i("hehe", "heheh");
3838
Intent intent=new Intent(MainActivity.this,RxJavaMainActivity.class);
3939
startActivity(intent);
4040
break;
4141
case R.id.btn_retrofit:
42+
Intent retrofit=new Intent(MainActivity.this,RxJavaRetrofitActivity.class);
43+
startActivity(retrofit);
44+
break;
45+
case R.id.btn_rxandroid:
46+
Intent rxandroid=new Intent(MainActivity.this,RxAndroidActivity.class);
47+
startActivity(rxandroid);
4248
break;
4349
}
4450
}

app/src/main/java/com/demo/maat/hello_rxjava/Operators2Fragment.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import butterknife.OnClick;
1818
import rx.Observable;
1919
import rx.Subscriber;
20+
import rx.Subscription;
2021
import rx.android.schedulers.AndroidSchedulers;
2122
import rx.functions.Action1;
2223
import rx.functions.Func1;
@@ -39,7 +40,7 @@ public class Operators2Fragment extends Fragment {
3940

4041

4142
private CompositeSubscription mCompositeSubscription;
42-
;
43+
Subscription mSubscription;
4344

4445
@Override
4546
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -62,15 +63,6 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
6263
}
6364

6465

65-
private void printLog(String s) {
66-
Log.i(TAG, s);
67-
}
68-
69-
@Override
70-
public void onDestroy() {
71-
super.onDestroy();
72-
mCompositeSubscription.unsubscribe();
73-
}
7466

7567
@OnClick({R.id.btn_buffer, R.id.btn_flarmap, R.id.btn_concatmap, R.id.btn_switchmap})
7668
public void onClick(View view) {
@@ -152,7 +144,7 @@ public void call(String s) {
152144

153145
//一共发送10次信息,Observable每次缓存3秒一起发送
154146
private void doBufferOperation() {
155-
final Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
147+
mSubscription=Observable.create(new Observable.OnSubscribe<String>() {
156148
@Override
157149
public void call(Subscriber<? super String> subscriber) {
158150
for (int i = 0; i <10 ; i++) {
@@ -164,17 +156,20 @@ public void call(Subscriber<? super String> subscriber) {
164156
}
165157
}
166158
}
167-
}).subscribeOn(Schedulers.io());
168-
observable.buffer(3, TimeUnit.SECONDS).subscribe(new Action1<List<String>>() {
169-
@Override
170-
public void call(List<String> list) {
171-
printLog("接受到 "+list.size()+"次");
172-
for (int i = 0; i < list.size(); i++){
173-
printLog(list.get(i));
174-
}
159+
}).subscribeOn(Schedulers.io())
160+
.buffer(3,TimeUnit.SECONDS)
161+
.subscribe(new Action1<List<String>>() {
162+
@Override
163+
public void call(List<String> list) {
164+
printLog("接收到 "+list.size()+"次");
165+
for (int i = 0; i < list.size(); i++){
166+
printLog(list.get(i));
167+
}
168+
169+
}
170+
});
171+
mCompositeSubscription.add(mSubscription);
175172

176-
}
177-
});
178173

179174
}
180175

@@ -185,6 +180,17 @@ public String[] getData(){
185180
return data;
186181
}
187182
}
183+
184+
185+
private void printLog(String s) {
186+
Log.i(TAG, s);
187+
}
188+
189+
@Override
190+
public void onDestroy() {
191+
super.onDestroy();
192+
mCompositeSubscription.unsubscribe();
193+
}
188194
}
189195

190196

app/src/main/java/com/demo/maat/hello_rxjava/PollingFragment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class PollingFragment extends Fragment {
3333
int N=0;
3434
private Subscription subscribe;
3535
private CompositeSubscription mCompositeSubscription;
36-
;
3736

3837
@Override
3938
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -59,7 +58,7 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
5958
@OnClick(R.id.btn_polling)
6059
public void onClick() {
6160

62-
61+
//此处有bug,subscribe无法释放,望高人pull requests
6362
subscribe=Observable.create(new Observable.OnSubscribe<String>() {
6463
@Override
6564
public void call(final Subscriber<? super String> observer) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package com.demo.maat.hello_rxjava;
19+
20+
import android.os.Bundle;
21+
import android.support.v4.app.FragmentTransaction;
22+
23+
import com.demo.maat.hello_rxjava.common.activities.SampleActivityBase;
24+
import com.demo.maat.hello_rxjava.common.logger.Log;
25+
import com.demo.maat.hello_rxjava.common.logger.LogFragment;
26+
import com.demo.maat.hello_rxjava.common.logger.LogWrapper;
27+
import com.demo.maat.hello_rxjava.common.logger.MessageOnlyLogFilter;
28+
29+
30+
public class RxAndroidActivity extends SampleActivityBase {
31+
32+
public static final String TAG = "RxAndroidActivity";
33+
34+
35+
@Override
36+
protected void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
setContentView(R.layout.scheduler_activity_main);
39+
40+
if (savedInstanceState == null) {
41+
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
42+
RxAndroidFragment fragment = new RxAndroidFragment();
43+
transaction.replace(R.id.sample_content_fragment, fragment);
44+
transaction.commit();
45+
}
46+
}
47+
48+
49+
50+
51+
@Override
52+
public void initializeLogging() {
53+
LogWrapper logWrapper = new LogWrapper();
54+
Log.setLogNode(logWrapper);
55+
56+
MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
57+
logWrapper.setNext(msgFilter);
58+
59+
// On screen logging via a fragment with a TextView.
60+
LogFragment logFragment = (LogFragment) getSupportFragmentManager()
61+
.findFragmentById(R.id.log_fragment);
62+
msgFilter.setNext(logFragment.getLogView());
63+
64+
Log.i(TAG, "Ready");
65+
}
66+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package com.demo.maat.hello_rxjava;
2+
3+
import android.os.Bundle;
4+
import android.os.HandlerThread;
5+
import android.os.Looper;
6+
import android.support.v4.app.Fragment;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.widget.Button;
11+
import android.widget.ProgressBar;
12+
13+
import com.demo.maat.hello_rxjava.common.logger.Log;
14+
15+
import java.util.concurrent.TimeUnit;
16+
17+
import butterknife.BindView;
18+
import butterknife.ButterKnife;
19+
import butterknife.OnClick;
20+
import rx.Observable;
21+
import rx.Subscriber;
22+
import rx.Subscription;
23+
import rx.android.schedulers.AndroidSchedulers;
24+
import rx.exceptions.OnErrorThrowable;
25+
import rx.functions.Func0;
26+
import rx.subscriptions.CompositeSubscription;
27+
28+
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
29+
30+
31+
public class RxAndroidFragment extends Fragment {
32+
33+
34+
static final String TAG = "RxAndroidFragment";
35+
@BindView(R.id.btn_rxandroid)
36+
Button mBtnPolling;
37+
@BindView(R.id.progress_operation_two_running)
38+
ProgressBar mProgressOperationTwoRunning;
39+
40+
private Looper backgroundLooper;
41+
42+
43+
private Subscription subscribe;
44+
private CompositeSubscription mCompositeSubscription;
45+
46+
47+
@Override
48+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
49+
Bundle savedInstanceState) {
50+
View view = inflater.inflate(R.layout.rxandroid_fragment, container, false);
51+
ButterKnife.bind(this, view);
52+
mCompositeSubscription = new CompositeSubscription();
53+
54+
BackgroundThread backgroundThread = new BackgroundThread();
55+
backgroundThread.start();
56+
backgroundLooper = backgroundThread.getLooper();
57+
58+
return view;
59+
}
60+
61+
62+
@OnClick(R.id.btn_rxandroid)
63+
public void onClick() {
64+
onRunSchedulerExampleButtonClicked();
65+
}
66+
67+
void onRunSchedulerExampleButtonClicked() {
68+
69+
sampleObservable()
70+
// Run on a background thread
71+
.subscribeOn(AndroidSchedulers.from(backgroundLooper))
72+
// Be notified on the main thread
73+
.observeOn(AndroidSchedulers.mainThread())
74+
.subscribe(new Subscriber<String>() {
75+
@Override
76+
public void onCompleted() {
77+
printLog("Completed");
78+
mProgressOperationTwoRunning.setVisibility(View.INVISIBLE);
79+
}
80+
81+
@Override
82+
public void onError(Throwable e) {
83+
printLog("Error");
84+
mProgressOperationTwoRunning.setVisibility(View.INVISIBLE);
85+
}
86+
87+
@Override
88+
public void onNext(String string) {
89+
mProgressOperationTwoRunning.setVisibility(View.INVISIBLE);
90+
printLog("Next " + string);
91+
}
92+
});
93+
}
94+
95+
Observable<String> sampleObservable() {
96+
mProgressOperationTwoRunning.setVisibility(View.VISIBLE);
97+
98+
return Observable.defer(new Func0<Observable<String>>() {
99+
@Override
100+
public Observable<String> call() {
101+
try {
102+
// Do some long running operation
103+
Thread.sleep(TimeUnit.SECONDS.toMillis(5));
104+
} catch (InterruptedException e) {
105+
throw OnErrorThrowable.from(e);
106+
}
107+
return Observable.just("one", "two", "three", "four", "five");
108+
}
109+
});
110+
}
111+
112+
private void printLog(String s) {
113+
Log.i(TAG, s);
114+
}
115+
116+
@Override
117+
public void onDestroy() {
118+
super.onDestroy();
119+
mCompositeSubscription.unsubscribe();
120+
}
121+
122+
123+
static class BackgroundThread extends HandlerThread {
124+
BackgroundThread() {
125+
super("SchedulerSample-BackgroundThread", THREAD_PRIORITY_BACKGROUND);
126+
}
127+
}
128+
}
129+
130+
131+
132+

0 commit comments

Comments
 (0)