|
| 1 | +/* |
| 2 | + Android Asynchronous Http Client Sample |
| 3 | + Copyright (c) 2014 Marek Sebera <[email protected]> |
| 4 | + http://loopj.com |
| 5 | +
|
| 6 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + you may not use this file except in compliance with the License. |
| 8 | + You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package com.loopj.android.http.sample; |
| 20 | + |
| 21 | +import android.app.Activity; |
| 22 | +import android.os.Looper; |
| 23 | +import android.util.Log; |
| 24 | +import android.widget.Toast; |
| 25 | + |
| 26 | +import com.loopj.android.http.AsyncHttpClient; |
| 27 | +import com.loopj.android.http.AsyncHttpResponseHandler; |
| 28 | +import com.loopj.android.http.RequestHandle; |
| 29 | +import com.loopj.android.http.ResponseHandlerInterface; |
| 30 | + |
| 31 | +import org.apache.http.Header; |
| 32 | +import org.apache.http.HttpEntity; |
| 33 | + |
| 34 | +import java.util.concurrent.Callable; |
| 35 | +import java.util.concurrent.ExecutorService; |
| 36 | +import java.util.concurrent.Executors; |
| 37 | +import java.util.concurrent.FutureTask; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +public class AsyncBackgroundThreadSample extends SampleParentActivity { |
| 41 | + private static final String LOG_TAG = "AsyncBackgroundThreadSample"; |
| 42 | + |
| 43 | + private ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 44 | + |
| 45 | + @Override |
| 46 | + public void onStop() |
| 47 | + { |
| 48 | + super.onStop(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public RequestHandle executeSample(final AsyncHttpClient client, final String URL, final Header[] headers, HttpEntity entity, final ResponseHandlerInterface responseHandler) { |
| 53 | + |
| 54 | + final Activity ctx = this; |
| 55 | + FutureTask<RequestHandle> future = new FutureTask<>(new Callable<RequestHandle>() { |
| 56 | + public RequestHandle call() { |
| 57 | + Log.d(LOG_TAG, "Executing GET request on background thread"); |
| 58 | + return client.get(null, URL, headers, null, responseHandler); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + executor.execute(future); |
| 63 | + |
| 64 | + RequestHandle handle = null; |
| 65 | + try { |
| 66 | + handle = future.get(5, TimeUnit.SECONDS); |
| 67 | + Log.d(LOG_TAG, "Background thread for GET request has finished"); |
| 68 | + } catch (Exception e) { |
| 69 | + Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); |
| 70 | + e.printStackTrace(); |
| 71 | + } |
| 72 | + |
| 73 | + return handle; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public int getSampleTitle() { |
| 78 | + return R.string.title_async_background_thread; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean isRequestBodyAllowed() { |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean isRequestHeadersAllowed() { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public String getDefaultURL() { |
| 93 | + return "https://httpbin.org/get"; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public ResponseHandlerInterface getResponseHandler() { |
| 98 | + |
| 99 | + FutureTask<ResponseHandlerInterface> future = new FutureTask<>(new Callable<ResponseHandlerInterface>() { |
| 100 | + |
| 101 | + @Override |
| 102 | + public ResponseHandlerInterface call() throws Exception { |
| 103 | + Log.d(LOG_TAG, "Creating AsyncHttpResponseHandler on background thread"); |
| 104 | + return new AsyncHttpResponseHandler(Looper.getMainLooper()) { |
| 105 | + |
| 106 | + @Override |
| 107 | + public void onStart() { |
| 108 | + clearOutputs(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onSuccess(int statusCode, Header[] headers, byte[] response) { |
| 113 | + Log.d(LOG_TAG, String.format("onSuccess executing on main thread : %B", Looper.myLooper() == Looper.getMainLooper())); |
| 114 | + debugHeaders(LOG_TAG, headers); |
| 115 | + debugStatusCode(LOG_TAG, statusCode); |
| 116 | + debugResponse(LOG_TAG, new String(response)); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) { |
| 121 | + Log.d(LOG_TAG, String.format("onFailure executing on main thread : %B", Looper.myLooper() == Looper.getMainLooper())); |
| 122 | + debugHeaders(LOG_TAG, headers); |
| 123 | + debugStatusCode(LOG_TAG, statusCode); |
| 124 | + debugThrowable(LOG_TAG, e); |
| 125 | + if (errorResponse != null) { |
| 126 | + debugResponse(LOG_TAG, new String(errorResponse)); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void onRetry(int retryNo) { |
| 132 | + Toast.makeText(AsyncBackgroundThreadSample.this, |
| 133 | + String.format("Request is retried, retry no. %d", retryNo), |
| 134 | + Toast.LENGTH_SHORT) |
| 135 | + .show(); |
| 136 | + } |
| 137 | + }; |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + executor.execute(future); |
| 142 | + |
| 143 | + ResponseHandlerInterface responseHandler = null; |
| 144 | + try { |
| 145 | + responseHandler = future.get(); |
| 146 | + Log.d(LOG_TAG, "Background thread for AsyncHttpResponseHandler has finished"); |
| 147 | + } catch (Exception e) { |
| 148 | + e.printStackTrace(); |
| 149 | + } |
| 150 | + |
| 151 | + return responseHandler; |
| 152 | + } |
| 153 | +} |
0 commit comments