|
| 1 | +package com.morihacky.android.rxjava.volley; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.os.Handler; |
| 5 | +import android.os.Looper; |
| 6 | +import android.support.annotation.Nullable; |
| 7 | +import android.util.Log; |
| 8 | +import android.view.LayoutInflater; |
| 9 | +import android.view.View; |
| 10 | +import android.view.ViewGroup; |
| 11 | +import android.widget.ListView; |
| 12 | +import butterknife.Bind; |
| 13 | +import butterknife.ButterKnife; |
| 14 | +import butterknife.OnClick; |
| 15 | +import com.android.volley.Request; |
| 16 | +import com.android.volley.VolleyError; |
| 17 | +import com.android.volley.toolbox.JsonObjectRequest; |
| 18 | +import com.android.volley.toolbox.RequestFuture; |
| 19 | +import com.morihacky.android.rxjava.R; |
| 20 | +import com.morihacky.android.rxjava.fragments.BaseFragment; |
| 21 | +import com.morihacky.android.rxjava.wiring.LogAdapter; |
| 22 | +import java.nio.charset.Charset; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.ExecutionException; |
| 26 | +import org.json.JSONObject; |
| 27 | +import rx.Observable; |
| 28 | +import rx.Observer; |
| 29 | +import rx.android.schedulers.AndroidSchedulers; |
| 30 | +import rx.functions.Func0; |
| 31 | +import rx.schedulers.Schedulers; |
| 32 | +import rx.subscriptions.CompositeSubscription; |
| 33 | +import timber.log.Timber; |
| 34 | + |
| 35 | +public class VolleyDemoFragment |
| 36 | + extends BaseFragment { |
| 37 | + |
| 38 | + public static final String TAG = "VolleyDemoFragment"; |
| 39 | + |
| 40 | + @Bind(R.id.list_threading_log) ListView _logsList; |
| 41 | + |
| 42 | + private List<String> _logs; |
| 43 | + private LogAdapter _adapter; |
| 44 | + |
| 45 | + private CompositeSubscription _compositeSubscription = new CompositeSubscription(); |
| 46 | + |
| 47 | + @Override |
| 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; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| 58 | + super.onActivityCreated(savedInstanceState); |
| 59 | + _setupLogger(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onPause() { |
| 64 | + super.onPause(); |
| 65 | + _compositeSubscription.unsubscribe(); |
| 66 | + } |
| 67 | + |
| 68 | + public Observable<JSONObject> newGetRouteData() { |
| 69 | + return Observable.defer(new Func0<Observable<JSONObject>>() { |
| 70 | + @Override |
| 71 | + public Observable<JSONObject> call() { |
| 72 | + try { |
| 73 | + return Observable.just(getRouteData()); |
| 74 | + } catch (InterruptedException | ExecutionException e) { |
| 75 | + Log.e("routes", e.getMessage()); |
| 76 | + return Observable.error(e); |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 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 | + |
| 126 | + // ----------------------------------------------------------------------------------- |
| 127 | + // Methods that help wiring up the example (irrelevant to RxJava) |
| 128 | + |
| 129 | + private void _setupLogger() { |
| 130 | + _logs = new ArrayList<>(); |
| 131 | + _adapter = new LogAdapter(getActivity(), new ArrayList<String>()); |
| 132 | + _logsList.setAdapter(_adapter); |
| 133 | + } |
| 134 | + |
| 135 | + private void _log(String logMsg) { |
| 136 | + |
| 137 | + if (_isCurrentlyOnMainThread()) { |
| 138 | + _logs.add(0, logMsg + " (main thread) "); |
| 139 | + _adapter.clear(); |
| 140 | + _adapter.addAll(_logs); |
| 141 | + } else { |
| 142 | + _logs.add(0, logMsg + " (NOT main thread) "); |
| 143 | + |
| 144 | + // You can only do below stuff on main thread. |
| 145 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 146 | + |
| 147 | + @Override |
| 148 | + public void run() { |
| 149 | + _adapter.clear(); |
| 150 | + _adapter.addAll(_logs); |
| 151 | + } |
| 152 | + }); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private boolean _isCurrentlyOnMainThread() { |
| 157 | + return Looper.myLooper() == Looper.getMainLooper(); |
| 158 | + } |
| 159 | +} |
0 commit comments