Skip to content

Commit 6b70be6

Browse files
committed
fix: correctly handle null responses from requests
1 parent c5643b5 commit 6b70be6

File tree

4 files changed

+119
-256
lines changed

4 files changed

+119
-256
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public OkHttpResponse(ResponseBody body) {
5353
public void cancel() {
5454
cancelled = true;
5555
}
56+
public long contentLength() {
57+
return responseBody.contentLength();
58+
}
5659

5760
static void runProgressCallback(final OkHttpResponseProgressCallback progressCallback, final long current,
5861
final long total) {
@@ -151,6 +154,7 @@ private static void closeResponseBody(ResponseBody responseBody, OkHttpResponseC
151154

152155
static Bitmap responseBodyToBitmap(OkHttpResponse response, OkHttpResponseProgressCallback progressCallback)
153156
throws Exception {
157+
154158
BufferedInputStream input = null;
155159
OutputStream output = null;
156160
try {
@@ -297,6 +301,7 @@ public void run() {
297301
}
298302

299303
static java.nio.ByteBuffer responseBodyToByteArray(OkHttpResponse response) throws IOException {
304+
300305
final byte[] result = response.responseBody.bytes();
301306
response.closeResponseBody();
302307
return java.nio.ByteBuffer.wrap(result);
@@ -347,6 +352,7 @@ public void run() {
347352
}
348353

349354
static String responseBodyToString(OkHttpResponse response) throws IOException {
355+
350356
final String responseString = response.responseBody.string();
351357
response.closeResponseBody();
352358
return responseString;

src/https.android.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,12 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
122122
jsonResponse: any;
123123
toJSON(encoding?: HttpResponseEncoding) {
124124
try {
125-
if (this.jsonResponse) {
125+
if (this.jsonResponse !== undefined) {
126126
return this.jsonResponse;
127127
}
128128
// TODO: handle arraybuffer already stored
129129
this.stringResponse = this.stringResponse || this.response.asString();
130-
this.jsonResponse = Https.parseJSON(this.stringResponse);
130+
this.jsonResponse = this.stringResponse ? Https.parseJSON(this.stringResponse) : null;
131131
return this.jsonResponse;
132132
} catch (err) {
133133
console.error('HttpsResponse.toJSON', err);
@@ -136,16 +136,16 @@ class HttpsResponse implements Https.HttpsResponseLegacy {
136136
}
137137

138138
async toJSONAsync() {
139-
if (this.jsonResponse) {
139+
if (this.jsonResponse !== undefined) {
140140
return this.jsonResponse;
141141
}
142-
if (this.stringResponse) {
143-
this.jsonResponse = Https.parseJSON(this.stringResponse);
142+
if (this.stringResponse !== undefined) {
143+
this.jsonResponse = this.stringResponse ? Https.parseJSON(this.stringResponse) : null;
144144
return this.jsonResponse;
145145
}
146146
// TODO: handle arraybuffer already stored
147147
const r = await this.toStringAsync();
148-
this.jsonResponse = Https.parseJSON(r);
148+
this.jsonResponse = r ? Https.parseJSON(r) : null;
149149
return this.jsonResponse;
150150
}
151151

0 commit comments

Comments
 (0)