Skip to content

Commit b2d3ee5

Browse files
authored
Merge pull request #12 from nstudio/fix/android-urlencoded
fix(android): urlencoded
2 parents 6e68028 + 58cfe7e commit b2d3ee5

File tree

3 files changed

+81
-15
lines changed

3 files changed

+81
-15
lines changed

src/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-http-async",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Http async multi-threaded for NativeScript",
55
"main": "async",
66
"typings": "index.d.ts",

src/platforms/android/java/com/github/triniwiz/async/Async.java

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
import java.io.IOException;
1919
import java.net.SocketTimeoutException;
2020
import java.nio.ByteBuffer;
21+
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2123
import java.util.ArrayList;
2224
import java.util.Collections;
2325
import java.util.Comparator;
26+
import java.util.Iterator;
2427
import java.util.UUID;
2528
import java.util.concurrent.ConcurrentHashMap;
2629
import java.util.concurrent.Executor;
@@ -97,6 +100,7 @@ public static class FileResult {
97100
}
98101
}
99102

103+
100104
static class ProgressRequestBody extends RequestBody {
101105
RequestBody body;
102106
ProgressListener listener;
@@ -354,19 +358,78 @@ public Request authenticate(Route route, Response response) throws IOException {
354358
if (options.content instanceof File) {
355359

356360
} else if (options.content instanceof String) {
357-
body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), (String) options.content), new ProgressListener() {
358-
@Override
359-
public void onProgress(long loaded, long total) {
360-
callback.onProgress(total > -1, loaded, total);
361+
if (contentType.equals("application/x-www-form-urlencoded")) {
362+
JSONTokener tokener = new JSONTokener((String) options.content);
363+
Object value = null;
364+
try {
365+
value = tokener.nextValue();
366+
} catch (JSONException e) {
367+
e.printStackTrace();
361368
}
362-
});
369+
if (value instanceof JSONObject) {
370+
FormBody.Builder formBody = new FormBody.Builder(StandardCharsets.UTF_8);
371+
372+
for (Iterator<String> it = ((JSONObject) value).keys(); it.hasNext(); ) {
373+
String key = it.next();
374+
formBody.addEncoded(key, String.valueOf(((JSONObject) value).opt(key)));
375+
}
376+
body = new ProgressRequestBody(formBody.build(), new ProgressListener() {
377+
@Override
378+
public void onProgress(long loaded, long total) {
379+
callback.onProgress(total > -1, loaded, total);
380+
}
381+
});
382+
}else {
383+
body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), (String) options.content), new ProgressListener() {
384+
@Override
385+
public void onProgress(long loaded, long total) {
386+
callback.onProgress(total > -1, loaded, total);
387+
}
388+
});
389+
}
390+
391+
} else {
392+
body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), (String) options.content), new ProgressListener() {
393+
@Override
394+
public void onProgress(long loaded, long total) {
395+
callback.onProgress(total > -1, loaded, total);
396+
}
397+
});
398+
}
399+
363400
} else if (options.content instanceof JSONObject || options.content instanceof JSONArray) {
364-
body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), options.content.toString()), new ProgressListener() {
365-
@Override
366-
public void onProgress(long loaded, long total) {
367-
callback.onProgress(total > -1, loaded, total);
401+
if (contentType.equals("application/x-www-form-urlencoded")) {
402+
if (options.content instanceof JSONObject) {
403+
FormBody.Builder formBody = new FormBody.Builder(StandardCharsets.UTF_8);
404+
405+
for (Iterator<String> it = (((JSONObject) options.content).keys()); it.hasNext(); ) {
406+
String key = it.next();
407+
formBody.addEncoded(key, String.valueOf(((JSONObject) options.content).opt(key)));
408+
}
409+
body = new ProgressRequestBody(formBody.build(), new ProgressListener() {
410+
@Override
411+
public void onProgress(long loaded, long total) {
412+
callback.onProgress(total > -1, loaded, total);
413+
}
414+
});
415+
} else {
416+
body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), options.content.toString()), new ProgressListener() {
417+
@Override
418+
public void onProgress(long loaded, long total) {
419+
callback.onProgress(total > -1, loaded, total);
420+
}
421+
});
368422
}
369-
});
423+
424+
}else {
425+
body = new ProgressRequestBody(RequestBody.create(MediaType.parse(contentType), options.content.toString()), new ProgressListener() {
426+
@Override
427+
public void onProgress(long loaded, long total) {
428+
callback.onProgress(total > -1, loaded, total);
429+
}
430+
});
431+
}
432+
370433
} else {
371434
body = RequestBody.create(null, "");
372435
}
@@ -409,14 +472,17 @@ public void onProgress(long loaded, long total) {
409472
}
410473
}
411474
});
412-
String contentType = call.request().header("Content-Type");
475+
String contentType = response.header("Content-Type");
413476
if (contentType == null) {
414-
contentType = call.request().header("content-Type");
477+
contentType = response.header("content-type");
415478
}
416479
String acceptHeader;
417480

418481
if (contentType == null) {
419-
acceptHeader = call.request().header("Accept");
482+
acceptHeader = response.header("Accept");
483+
if (acceptHeader == null){
484+
acceptHeader = response.header("accept");
485+
}
420486
} else {
421487
acceptHeader = contentType;
422488
}

0 commit comments

Comments
 (0)