Skip to content

Commit a25196a

Browse files
committed
added listview output to Retrofit example
1 parent 1ccb0cf commit a25196a

File tree

3 files changed

+168
-110
lines changed

3 files changed

+168
-110
lines changed

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

Lines changed: 158 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,28 @@
33
import android.os.Bundle;
44
import android.support.annotation.Nullable;
55
import android.support.v4.app.Fragment;
6+
import android.text.TextUtils;
7+
import android.util.Base64;
68
import android.view.LayoutInflater;
79
import android.view.View;
810
import android.view.ViewGroup;
11+
import android.widget.ArrayAdapter;
912
import android.widget.EditText;
10-
import butterknife.ButterKnife;
11-
import butterknife.InjectView;
12-
import butterknife.OnClick;
13+
import android.widget.ListView;
14+
1315
import com.google.common.base.Strings;
1416
import com.morihacky.android.rxjava.app.R;
1517
import com.morihacky.android.rxjava.retrofit.Contributor;
1618
import com.morihacky.android.rxjava.retrofit.GithubApi;
1719
import com.morihacky.android.rxjava.retrofit.User;
20+
21+
import java.util.ArrayList;
1822
import java.util.List;
23+
24+
import butterknife.ButterKnife;
25+
import butterknife.InjectView;
26+
import butterknife.OnClick;
27+
import retrofit.RequestInterceptor;
1928
import retrofit.RestAdapter;
2029
import rx.Observable;
2130
import rx.Observer;
@@ -25,111 +34,150 @@
2534
import rx.schedulers.Schedulers;
2635
import timber.log.Timber;
2736

28-
public class RetrofitFragment
29-
extends Fragment {
30-
31-
GithubApi api;
32-
33-
@InjectView(R.id.demo_retrofit_contributors_username) EditText _username;
34-
@InjectView(R.id.demo_retrofit_contributors_repository) EditText _repo;
35-
36-
@Override
37-
public void onCreate(Bundle savedInstanceState) {
38-
super.onCreate(savedInstanceState);
39-
40-
RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("https://api.github.com/")
41-
.build();
42-
43-
api = restAdapter.create(GithubApi.class);
44-
}
45-
46-
@Override
47-
public View onCreateView(LayoutInflater inflater,
48-
@Nullable ViewGroup container,
49-
@Nullable Bundle savedInstanceState) {
50-
View layout = inflater.inflate(R.layout.fragment_retrofit, container, false);
51-
ButterKnife.inject(this, layout);
52-
return layout;
53-
}
54-
55-
@OnClick(R.id.btn_demo_retrofit_contributors)
56-
public void onListContributorsClicked() {
57-
api.contributors(_username.getText().toString(), _repo.getText().toString())
58-
.subscribe(new Observer<List<Contributor>>() {
59-
@Override
60-
public void onCompleted() {
61-
Timber.d("Retrofit call 1 completed");
62-
}
63-
64-
@Override
65-
public void onError(Throwable e) {
66-
Timber.e(e, "woops we got an error while getting the list of contributors");
67-
}
68-
69-
@Override
70-
public void onNext(List<Contributor> contributors) {
71-
for (Contributor c : contributors) {
72-
Timber.d("%s has made %d contributions to %s",
73-
c.login,
74-
c.contributions,
75-
_repo.getText().toString());
76-
}
77-
}
78-
});
79-
}
80-
81-
@OnClick(R.id.btn_demo_retrofit_contributors_with_user_info)
82-
public void onListContributorsWithFullUserInfoClicked() {
83-
api.contributors(_username.getText().toString(), _repo.getText().toString())
84-
.flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
85-
@Override
86-
public Observable<Contributor> call(List<Contributor> contributors) {
87-
return Observable.from(contributors);
88-
}
89-
})
90-
.flatMap(new Func1<Contributor, Observable<?>>() {
91-
@Override
92-
public Observable<?> call(Contributor contributor) {
93-
Observable.zip(Observable.just(contributor),
94-
api.user(contributor.login).filter(new Func1<User, Boolean>() {
95-
@Override
96-
public Boolean call(User user) {
97-
return !Strings.isNullOrEmpty(user.name) && !Strings.isNullOrEmpty(user.email);
98-
}
99-
}),
100-
new Func2<Contributor, User, Object>() {
101-
@Override
102-
public Object call(Contributor contributor, User user) {
103-
Timber.d("%s(%s) has made %d contributions to %s",
104-
user.name,
105-
user.email,
106-
contributor.contributions,
107-
_repo.getText().toString());
108-
109-
return Observable.empty();
110-
}
111-
}).subscribe();
112-
return Observable.empty();
113-
}
114-
})
115-
.subscribeOn(Schedulers.newThread())
116-
.observeOn(AndroidSchedulers.mainThread())
117-
.subscribe(new Observer<Object>() {
118-
@Override
119-
public void onCompleted() {
120-
Timber.d("Retrofit call 2 completed ");
121-
}
122-
123-
@Override
124-
public void onError(Throwable e) {
125-
Timber.e(e,
126-
"woops we got an error while getting the list of contributors along with full names");
127-
}
128-
129-
@Override
130-
public void onNext(Object o) {
131-
Timber.d("hi! onNext");
132-
}
133-
});
134-
}
37+
public class RetrofitFragment extends Fragment {
38+
39+
private ArrayAdapter<String> _adapter;
40+
41+
GithubApi api;
42+
43+
@InjectView(R.id.log_list) ListView _resultsListView;
44+
@InjectView(R.id.demo_retrofit_contributors_username) EditText _username;
45+
@InjectView(R.id.demo_retrofit_contributors_repository) EditText _repo;
46+
47+
@Override
48+
public void onCreate(Bundle savedInstanceState) {
49+
super.onCreate(savedInstanceState);
50+
51+
final String githubUsernamePassword = getActivity().getString(R.string.github_username_password);
52+
53+
RestAdapter.Builder builder = new RestAdapter.Builder()
54+
.setEndpoint("https://api.github.com/")
55+
.setLogLevel(RestAdapter.LogLevel.FULL);
56+
57+
if (!TextUtils.isEmpty(githubUsernamePassword)) {
58+
builder.setRequestInterceptor(new RequestInterceptor() {
59+
@Override
60+
public void intercept(RequestFacade request) {
61+
String string = "Basic " + Base64.encodeToString(githubUsernamePassword.getBytes(), Base64.NO_WRAP);
62+
request.addHeader("Accept", "application/json");
63+
request.addHeader("Authorization", string);
64+
}
65+
});
66+
}
67+
68+
RestAdapter restAdapter = builder.build();
69+
70+
api = restAdapter.create(GithubApi.class);
71+
}
72+
73+
@Override
74+
public View onCreateView(LayoutInflater inflater,
75+
@Nullable ViewGroup container,
76+
@Nullable Bundle savedInstanceState) {
77+
View layout = inflater.inflate(R.layout.fragment_retrofit, container, false);
78+
ButterKnife.inject(this, layout);
79+
80+
81+
82+
_adapter = new ArrayAdapter<>(getActivity(), R.layout.item_log, R.id.item_log, new ArrayList<String>());
83+
_adapter.setNotifyOnChange(true);
84+
_resultsListView.setAdapter(_adapter);
85+
86+
return layout;
87+
}
88+
89+
@OnClick(R.id.btn_demo_retrofit_contributors)
90+
public void onListContributorsClicked() {
91+
_adapter.clear();
92+
api.contributors(_username.getText().toString(), _repo.getText().toString())
93+
.subscribeOn(Schedulers.io())
94+
.observeOn(AndroidSchedulers.mainThread())
95+
.subscribe(new Observer<List<Contributor>>() {
96+
@Override
97+
public void onCompleted() {
98+
Timber.d("Retrofit call 1 completed");
99+
}
100+
101+
@Override
102+
public void onError(Throwable e) {
103+
Timber.e(e, "woops we got an error while getting the list of contributors");
104+
}
105+
106+
@Override
107+
public void onNext(List<Contributor> contributors) {
108+
for (Contributor c : contributors) {
109+
_adapter.add(String.format("%s has made %d contributions to %s",
110+
c.login,
111+
c.contributions,
112+
_repo.getText().toString()));
113+
Timber.d("%s has made %d contributions to %s",
114+
c.login,
115+
c.contributions,
116+
_repo.getText().toString());
117+
}
118+
}
119+
});
120+
}
121+
122+
@OnClick(R.id.btn_demo_retrofit_contributors_with_user_info)
123+
public void onListContributorsWithFullUserInfoClicked() {
124+
_adapter.clear();
125+
api.contributors(_username.getText().toString(), _repo.getText().toString())
126+
.flatMap(new Func1<List<Contributor>, Observable<Contributor>>() {
127+
@Override
128+
public Observable<Contributor> call(List<Contributor> contributors) {
129+
return Observable.from(contributors);
130+
}
131+
})
132+
.flatMap(new Func1<Contributor, Observable<?>>() {
133+
@Override
134+
public Observable<?> call(Contributor contributor) {
135+
Observable.zip(Observable.just(contributor),
136+
api.user(contributor.login).filter(new Func1<User, Boolean>() {
137+
@Override
138+
public Boolean call(User user) {
139+
return !Strings.isNullOrEmpty(user.name) && !Strings.isNullOrEmpty(user.email);
140+
}
141+
}),
142+
new Func2<Contributor, User, Object>() {
143+
@Override
144+
public Object call(Contributor contributor, User user) {
145+
_adapter.add(String.format("%s(%s) has made %d contributions to %s",
146+
user.name,
147+
user.email,
148+
contributor.contributions,
149+
_repo.getText().toString()));
150+
_adapter.notifyDataSetChanged();
151+
Timber.d("%s(%s) has made %d contributions to %s",
152+
user.name,
153+
user.email,
154+
contributor.contributions,
155+
_repo.getText().toString());
156+
157+
return Observable.empty();
158+
}
159+
}).subscribe();
160+
return Observable.empty();
161+
}
162+
})
163+
.subscribeOn(Schedulers.newThread())
164+
.observeOn(AndroidSchedulers.mainThread())
165+
.subscribe(new Observer<Object>() {
166+
@Override
167+
public void onCompleted() {
168+
Timber.d("Retrofit call 2 completed ");
169+
}
170+
171+
@Override
172+
public void onError(Throwable e) {
173+
Timber.e(e,
174+
"woops we got an error while getting the list of contributors along with full names");
175+
}
176+
177+
@Override
178+
public void onNext(Object o) {
179+
Timber.d("hi! onNext");
180+
}
181+
});
182+
}
135183
}

app/src/main/res/layout/fragment_retrofit.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,10 @@
7474
android:hint="reponame"/>
7575
</LinearLayout>
7676

77+
<ListView
78+
android:id="@+id/log_list"
79+
android:layout_width="match_parent"
80+
android:layout_height="match_parent">
81+
</ListView>
82+
7783
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@
2222
<string name="msg_demo_doublebinding">Watch how the result gloriously auto-updates based on your changing inputs. Using a technique like this, you could achieve the two-way binding in Angular Js, or more efficiently use a pattern like the Presentation View Model.</string>
2323
<string name="msg_demo_polling">This is demo of polling or making a call repeatedly with RxJava. \n\nSimple polling: Notice in the logs how a network call (simulated) is repeatedly made in the background.</string>
2424
<string name="msg_demo_rxbus_1">Tap on the below button and RxBus will listen to the events</string>
25+
26+
<!-- format username:password -->
27+
<!--suppress CheckTagEmptyBody -->
28+
<string name="github_username_password"></string>
2529
</resources>

0 commit comments

Comments
 (0)