Skip to content

Commit bb72427

Browse files
committed
feat(android)!: okhttp 4.x. minSDKVersion changed to 21
1 parent 78be199 commit bb72427

File tree

8 files changed

+5606
-2746
lines changed

8 files changed

+5606
-2746
lines changed

README.md

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
# @nativescript-community/https
22

3-
[![NPM version][npm-image]][npm-url]
4-
[![Downloads][downloads-image]][npm-url]
5-
[![TotalDownloads][total-downloads-image]][npm-url]
6-
[![Twitter Follow][twitter-image]][twitter-url]
7-
8-
[build-status]: https://travis-ci.org/nativescript-community/https.svg?branch=master
9-
[build-url]: https://travis-ci.org/nativescript-community/https
10-
[npm-image]: http://img.shields.io/npm/v/@nativescript-community/https.svg
11-
[npm-url]: https://npmjs.org/package/@nativescript-community/https
12-
[downloads-image]: http://img.shields.io/npm/dm/@nativescript-community/https.svg
13-
[total-downloads-image]: http://img.shields.io/npm/dt/@nativescript-community/https.svg?label=total%20downloads
14-
[twitter-image]: https://img.shields.io/twitter/follow/eddyverbruggen.svg?style=social&label=Follow%20me
15-
[twitter-url]: https://twitter.com/eddyverbruggen
16-
173
### The definitive way to hit HTTP based APIs in Nativescript.
184

195
Easily integrate the most reliable native networking libraries with the latest and greatest HTTPS security features.
206

7+
> Android: version 4.x using okhttp 4.x changing minSDKVersion to 21! If lower needed stick to 3.x
8+
219
> Plugin version 2.0.0 bumps `AFNetworking` on iOS to [4.0.0](https://github.com/AFNetworking/AFNetworking/releases/tag/4.0.0) which no longer relies on `UIWebView`. Make sure to run `pod repo update` to get the latest `AFNetworking` pod on your development machine.
2210
23-
#### A drop-in replacement for the [default http module](https://docs.nativescript.org/cookbook/http#get-response-status-code).
11+
#### A drop-in replacement for the [default http module](https://docs.nativescript.org/cookbook/http).
2412

2513
## Features
2614

@@ -44,29 +32,9 @@ Easily integrate the most reliable native networking libraries with the latest a
4432
4533
**No.** This plugin works out of the box without any security configurations needed. Either way you'll still benefit from all the features listed above.
4634

47-
## Demo
48-
49-
```shell
50-
git clone https://github.com/nativescript-community/https
51-
cd https
52-
npm run demo.ios
53-
npm run demo.android
54-
```
5535

5636
## Installation
5737

58-
#### Add `tns-platform-declarations` for Android and iOS to your `references.d.ts`!
59-
60-
```typescript
61-
/// <reference path="./node_modules/tns-platform-declarations/android.d.ts" />
62-
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />
63-
```
64-
65-
We also recommend adding `"skipLibCheck": true,` to your `tsconfig.json`.
66-
More information on that can be found [here](https://github.com/NativeScript/NativeScript/tree/master/tns-platform-declarations).
67-
68-
Install the plugin:
69-
7038
```bash
7139
tns plugin add @nativescript-community/https
7240
```
@@ -227,3 +195,12 @@ then pass the option `allowLargeResponse` with value `true` to the `request` fun
227195
| [Robert Laverty](https://github.com/roblav96) | For creating and maintaining this plugin for a long time, before transfering it to me, with the help of Jeff Whelpley of [GetHuman](https://github.com/gethuman). |
228196
| [AFNetworking](https://github.com/AFNetworking) | [AFNetworking](https://github.com/AFNetworking/AFNetworking) A delightful networking framework for iOS, OS X, watchOS, and tvOS. |
229197
| [Square](http://square.github.io/) | [okhttp](https://github.com/square/okhttp) An HTTP+HTTP/2 client for Android and Java applications. |
198+
199+
## Demo
200+
201+
```shell
202+
git clone https://github.com/nativescript-community/https
203+
cd https
204+
npm run demo.ios
205+
npm run demo.android
206+
```

plugin/platforms/android/include.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dependencies {
2-
def okHttpVersion = project.hasProperty("okHttpVersion") ? project.okHttpVersion : "3.14.9"
2+
def okHttpVersion = project.hasProperty("okHttpVersion") ? project.okHttpVersion : "4.9.3"
33
implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
44
// def conscryptVersion = project.hasProperty("conscryptVersion") ? project.conscryptVersion : "2.4.0"
55
// implementation "org.conscrypt:conscrypt-android:$conscryptVersion"

plugin/platforms/android/java/com/nativescript/https/CacheInterceptor.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
import okhttp3.Response;
55
import java.io.IOException;
66

7-
public class CacheInterceptor implements Interceptor {
8-
final String TAG = "CacheInterceptor";
97

10-
public Response intercept(Interceptor.Chain chain ) throws IOException {
8+
public class CacheInterceptor {
9+
public static final Interceptor INTERCEPTOR = chain -> {
1110
Request originalRequest = chain.request();
1211
String cacheControlHeader = originalRequest.header("Cache-Control");
1312
Response originalResponse = chain.proceed(originalRequest);
@@ -16,7 +15,5 @@ public Response intercept(Interceptor.Chain chain ) throws IOException {
1615
} else {
1716
return originalResponse;
1817
}
19-
}
20-
21-
18+
};
2219
}

plugin/platforms/android/java/com/nativescript/https/OkHttpResponse.java

Lines changed: 100 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
import android.graphics.Bitmap;
1313
import android.graphics.BitmapFactory;
1414
import android.os.Handler;
15+
import android.os.Looper;
16+
import android.util.Log;
1517

1618
import okhttp3.ResponseBody;
19+
import okhttp3.Response;
1720

1821
public class OkHttpResponse {
1922
private final static String TAG = "OkHttpResponse";
@@ -43,6 +46,53 @@ public static interface OkHttpResponseProgressCallback {
4346
public static interface OkHttpResponseCloseCallback {
4447
void onClose();
4548
}
49+
private static class NotifyRunnable implements Runnable {
50+
private final Runnable mRunnable;
51+
private final Handler mHandler;
52+
private boolean mFinished = false;
53+
54+
public NotifyRunnable(final Handler handler, final Runnable r) {
55+
mRunnable = r;
56+
mHandler = handler;
57+
}
58+
59+
public boolean isFinished() {
60+
return mFinished;
61+
}
62+
63+
@Override
64+
public void run() {
65+
synchronized (mHandler) {
66+
Log.d("JS", "NotifyRunnable run");
67+
mRunnable.run();
68+
mFinished = true;
69+
mHandler.notifyAll();
70+
}
71+
}
72+
}
73+
74+
public static void postAndWait(final Handler handler, final Runnable r) {
75+
76+
if (handler.getLooper() == Looper.myLooper()) {
77+
r.run();
78+
} else {
79+
synchronized (handler) {
80+
Log.d("JS", "postAndWait1");
81+
NotifyRunnable runnable = new NotifyRunnable(handler, r);
82+
handler.post(runnable);
83+
Log.d("JS", "postAndWait2");
84+
while (!runnable.isFinished()) {
85+
Log.d("JS", "postAndWait3");
86+
try {
87+
handler.wait();
88+
} catch (InterruptedException is) {
89+
Log.d("JS", "postAndWait4", is);
90+
// ignore
91+
}
92+
}
93+
}
94+
}
95+
}
4696

4797
public OkHttpResponse(ResponseBody body) {
4898
responseBody = body;
@@ -57,16 +107,20 @@ public long contentLength() {
57107
return responseBody.contentLength();
58108
}
59109

110+
private static Handler getMainHandler() {
111+
if (mainHandler == null) {
112+
mainHandler = new Handler(android.os.Looper.getMainLooper());
113+
}
114+
return mainHandler;
115+
}
116+
60117
static void runProgressCallback(final OkHttpResponseProgressCallback progressCallback, final long current,
61118
final long total) {
62119
if (progressCallback == null) {
63120
return;
64121
}
65122
if (RUN_ON_MAIN_THREAD) {
66-
if (mainHandler == null) {
67-
mainHandler = new Handler(android.os.Looper.getMainLooper());
68-
}
69-
mainHandler.post(new Runnable() {
123+
getMainHandler().post(new Runnable() {
70124
@Override
71125
public void run() {
72126
progressCallback.onProgress(current, total);
@@ -82,10 +136,8 @@ static void runCloseCallback(final OkHttpResponseCloseCallback closeCallback) {
82136
return;
83137
}
84138
if (RUN_ON_MAIN_THREAD) {
85-
if (mainHandler == null) {
86-
mainHandler = new Handler(android.os.Looper.getMainLooper());
87-
}
88-
mainHandler.post(new Runnable() {
139+
140+
getMainHandler().post(new Runnable() {
89141
@Override
90142
public void run() {
91143
closeCallback.onClose();
@@ -213,10 +265,7 @@ public void run() {
213265
// Log.d(TAG, "toFileAsync run ");
214266
final File result = responseBodyToFile(filePath, fme, progressCallback);
215267
if (RUN_ON_MAIN_THREAD) {
216-
if (mainHandler == null) {
217-
mainHandler = new Handler(android.os.Looper.getMainLooper());
218-
}
219-
mainHandler.post(new Runnable() {
268+
getMainHandler().post(new Runnable() {
220269
@Override
221270
public void run() {
222271
callback.onFile(result);
@@ -227,10 +276,7 @@ public void run() {
227276
}
228277
} catch (final Exception exc) {
229278
if (RUN_ON_MAIN_THREAD) {
230-
if (mainHandler == null) {
231-
mainHandler = new Handler(android.os.Looper.getMainLooper());
232-
}
233-
mainHandler.post(new Runnable() {
279+
getMainHandler().post(new Runnable() {
234280
@Override
235281
public void run() {
236282
callback.onException(exc);
@@ -268,10 +314,7 @@ public void run() {
268314
try {
269315
final Bitmap result = responseBodyToBitmap(fme, progressCallback);
270316
if (RUN_ON_MAIN_THREAD) {
271-
if (mainHandler == null) {
272-
mainHandler = new Handler(android.os.Looper.getMainLooper());
273-
}
274-
mainHandler.post(new Runnable() {
317+
getMainHandler().post(new Runnable() {
275318
@Override
276319
public void run() {
277320
callback.onBitmap(result);
@@ -282,10 +325,7 @@ public void run() {
282325
}
283326
} catch (final Exception exc) {
284327
if (RUN_ON_MAIN_THREAD) {
285-
if (mainHandler == null) {
286-
mainHandler = new Handler(android.os.Looper.getMainLooper());
287-
}
288-
mainHandler.post(new Runnable() {
328+
getMainHandler().post(new Runnable() {
289329
@Override
290330
public void run() {
291331
callback.onException(exc);
@@ -319,10 +359,7 @@ public void run() {
319359
try {
320360
final java.nio.ByteBuffer result = responseBodyToByteArray(fme);
321361
if (RUN_ON_MAIN_THREAD) {
322-
if (mainHandler == null) {
323-
mainHandler = new Handler(android.os.Looper.getMainLooper());
324-
}
325-
mainHandler.post(new Runnable() {
362+
getMainHandler().post(new Runnable() {
326363
@Override
327364
public void run() {
328365
callback.onByteArray(result);
@@ -333,10 +370,7 @@ public void run() {
333370
}
334371
} catch (final Exception exc) {
335372
if (RUN_ON_MAIN_THREAD) {
336-
if (mainHandler == null) {
337-
mainHandler = new Handler(android.os.Looper.getMainLooper());
338-
}
339-
mainHandler.post(new Runnable() {
373+
getMainHandler().post(new Runnable() {
340374
@Override
341375
public void run() {
342376
callback.onException(exc);
@@ -359,7 +393,31 @@ static String responseBodyToString(OkHttpResponse response) throws IOException {
359393
}
360394

361395
public String asString() throws IOException {
362-
return responseBodyToString(this);
396+
// if (getMainHandler().getLooper() != Looper.myLooper()) {
397+
// try {
398+
return responseBodyToString(this);
399+
// } catch (final Exception ex) {
400+
// Log.d("JS", "RuntimeException " + ex);
401+
// return null;
402+
// }
403+
// }
404+
// final Object[] arr = new Object[1];
405+
// Log.d("JS", "test asString");
406+
// postAndWait(getMainHandler(), new Runnable() {
407+
// @Override
408+
// public void run() {
409+
// Log.d("JS", "test asString runnable");
410+
// try {
411+
// arr[1] = responseBodyToString(OkHttpResponse.this);
412+
// Log.d("JS", "test asString runnable result " + (String)arr[1]);
413+
// } catch (final Exception ex) {
414+
// Log.d("JS", "RuntimeException " + ex);
415+
// // throw new RuntimeException(ex);
416+
// }
417+
// }
418+
// });
419+
// Log.d("JS", "test asString result " + (String)arr[1]);
420+
// return (String)arr[1];
363421
}
364422

365423
public void asStringAsync(final OkHttpResponseAsyncCallback callback) {
@@ -370,10 +428,7 @@ public void run() {
370428
try {
371429
final String result = responseBodyToString(fme);
372430
if (RUN_ON_MAIN_THREAD) {
373-
if (mainHandler == null) {
374-
mainHandler = new Handler(android.os.Looper.getMainLooper());
375-
}
376-
mainHandler.post(new Runnable() {
431+
getMainHandler().post(new Runnable() {
377432
@Override
378433
public void run() {
379434
callback.onString(result);
@@ -384,10 +439,7 @@ public void run() {
384439
}
385440
} catch (final Exception exc) {
386441
if (RUN_ON_MAIN_THREAD) {
387-
if (mainHandler == null) {
388-
mainHandler = new Handler(android.os.Looper.getMainLooper());
389-
}
390-
mainHandler.post(new Runnable() {
442+
getMainHandler().post(new Runnable() {
391443
@Override
392444
public void run() {
393445
callback.onException(exc);
@@ -401,4 +453,11 @@ public void run() {
401453
});
402454
thread.start();
403455
}
456+
457+
public static int getStatusCode(Response response) {
458+
return response.code();
459+
}
460+
public static String getMessage(Response response) {
461+
return response.message();
462+
}
404463
}

0 commit comments

Comments
 (0)