1- package com .morihacky .android .rxjava .fragments ;
1+ package com .morihacky .android .rxjava .volley ;
22
33import android .os .Bundle ;
44import android .os .Handler ;
88import android .view .LayoutInflater ;
99import android .view .View ;
1010import android .view .ViewGroup ;
11- import android .widget .Button ;
1211import android .widget .ListView ;
13-
12+ import butterknife .Bind ;
13+ import butterknife .ButterKnife ;
14+ import butterknife .OnClick ;
1415import com .android .volley .Request ;
1516import com .android .volley .VolleyError ;
1617import com .android .volley .toolbox .JsonObjectRequest ;
1718import com .android .volley .toolbox .RequestFuture ;
1819import com .morihacky .android .rxjava .R ;
20+ import com .morihacky .android .rxjava .fragments .BaseFragment ;
1921import com .morihacky .android .rxjava .wiring .LogAdapter ;
20- import com .morihacky .android .rxjava .wiring .MyVolley ;
21-
22- import org .json .JSONObject ;
23-
2422import java .nio .charset .Charset ;
2523import java .util .ArrayList ;
2624import java .util .List ;
2725import java .util .concurrent .ExecutionException ;
28-
29- import butterknife .Bind ;
30- import butterknife .ButterKnife ;
31- import butterknife .OnClick ;
26+ import org .json .JSONObject ;
3227import rx .Observable ;
3328import rx .Observer ;
3429import rx .android .schedulers .AndroidSchedulers ;
3732import rx .subscriptions .CompositeSubscription ;
3833import timber .log .Timber ;
3934
40- /**
41- * Created by zhangxitao on 15/12/30.
42- */
43-
4435public 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
0 commit comments