|
36 | 36 |
|
37 | 37 | public class RetrofitFragment extends Fragment { |
38 | 38 |
|
39 | | - private ArrayAdapter<String> _adapter; |
| 39 | + private ArrayAdapter<String> _adapter; |
40 | 40 |
|
41 | | - GithubApi api; |
| 41 | + GithubApi api; |
42 | 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; |
| 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 | 46 |
|
47 | | - @Override |
48 | | - public void onCreate(Bundle savedInstanceState) { |
49 | | - super.onCreate(savedInstanceState); |
| 47 | + @Override |
| 48 | + public void onCreate(Bundle savedInstanceState) { |
| 49 | + super.onCreate(savedInstanceState); |
50 | 50 |
|
51 | | - final String githubUsernamePassword = getActivity().getString(R.string.github_username_password); |
| 51 | + final String githubUsernamePassword = getActivity().getString(R.string.github_username_password); |
52 | 52 |
|
53 | | - RestAdapter.Builder builder = new RestAdapter.Builder() |
54 | | - .setEndpoint("https://api.github.com/") |
55 | | - .setLogLevel(RestAdapter.LogLevel.FULL); |
| 53 | + RestAdapter.Builder builder = new RestAdapter.Builder() |
| 54 | + .setEndpoint("https://api.github.com/") |
| 55 | + .setLogLevel(RestAdapter.LogLevel.FULL); |
56 | 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 | | - }); |
| 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); |
66 | 64 | } |
67 | | - |
68 | | - RestAdapter restAdapter = builder.build(); |
69 | | - |
70 | | - api = restAdapter.create(GithubApi.class); |
| 65 | + }); |
71 | 66 | } |
72 | 67 |
|
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 | | - } |
| 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 | + } |
183 | 183 | } |
0 commit comments