|
1 | | -package com.morihacky.android.rxjava.fragments; |
| 1 | +package com.morihacky.android.rxjava.volley; |
2 | 2 |
|
3 | 3 | import android.os.Bundle; |
4 | 4 | import android.os.Handler; |
|
8 | 8 | import android.view.LayoutInflater; |
9 | 9 | import android.view.View; |
10 | 10 | import android.view.ViewGroup; |
11 | | -import android.widget.Button; |
12 | 11 | import android.widget.ListView; |
13 | | - |
| 12 | +import butterknife.Bind; |
| 13 | +import butterknife.ButterKnife; |
| 14 | +import butterknife.OnClick; |
14 | 15 | import com.android.volley.Request; |
15 | 16 | import com.android.volley.VolleyError; |
16 | 17 | import com.android.volley.toolbox.JsonObjectRequest; |
17 | 18 | import com.android.volley.toolbox.RequestFuture; |
18 | 19 | import com.morihacky.android.rxjava.R; |
| 20 | +import com.morihacky.android.rxjava.fragments.BaseFragment; |
19 | 21 | import com.morihacky.android.rxjava.wiring.LogAdapter; |
20 | | -import com.morihacky.android.rxjava.wiring.MyVolley; |
21 | | - |
22 | | -import org.json.JSONObject; |
23 | | - |
24 | 22 | import java.nio.charset.Charset; |
25 | 23 | import java.util.ArrayList; |
26 | 24 | import java.util.List; |
27 | 25 | import java.util.concurrent.ExecutionException; |
28 | | - |
29 | | -import butterknife.Bind; |
30 | | -import butterknife.ButterKnife; |
31 | | -import butterknife.OnClick; |
| 26 | +import org.json.JSONObject; |
32 | 27 | import rx.Observable; |
33 | 28 | import rx.Observer; |
34 | 29 | import rx.android.schedulers.AndroidSchedulers; |
|
37 | 32 | import rx.subscriptions.CompositeSubscription; |
38 | 33 | import timber.log.Timber; |
39 | 34 |
|
40 | | -/** |
41 | | - * Created by zhangxitao on 15/12/30. |
42 | | - */ |
43 | | - |
44 | 35 | public class VolleyDemoFragment |
45 | | - extends BaseFragment { |
| 36 | + extends BaseFragment { |
| 37 | + |
46 | 38 | public static final String TAG = "VolleyDemoFragment"; |
47 | 39 |
|
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; |
52 | 41 |
|
53 | | - private LogAdapter _adapter; |
54 | 42 | private List<String> _logs; |
| 43 | + private LogAdapter _adapter; |
55 | 44 |
|
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(); |
72 | 46 |
|
73 | 47 | @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; |
77 | 54 | } |
78 | 55 |
|
79 | 56 | @Override |
@@ -83,66 +60,15 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
83 | 60 | } |
84 | 61 |
|
85 | 62 | @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(); |
136 | 66 | } |
137 | 67 |
|
138 | | - /** |
139 | | - * @use the observable, same type data Jsob Object |
140 | | - */ |
141 | 68 | public Observable<JSONObject> newGetRouteData() { |
142 | 69 | return Observable.defer(new Func0<Observable<JSONObject>>() { |
143 | 70 | @Override |
144 | 71 | public Observable<JSONObject> call() { |
145 | | - Exception exception; |
146 | 72 | try { |
147 | 73 | return Observable.just(getRouteData()); |
148 | 74 | } catch (InterruptedException | ExecutionException e) { |
@@ -153,6 +79,50 @@ public Observable<JSONObject> call() { |
153 | 79 | }); |
154 | 80 | } |
155 | 81 |
|
| 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 | + |
156 | 126 | // ----------------------------------------------------------------------------------- |
157 | 127 | // Methods that help wiring up the example (irrelevant to RxJava) |
158 | 128 |
|
|