Skip to content

Commit ef8e28b

Browse files
committed
fix(android): responseText
1 parent c35c6c0 commit ef8e28b

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/http/http.android.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ function parseJSON(source: string): any {
6565
return JSON.parse(src);
6666
}
6767

68+
const textTypes: string[] = [
69+
'text/plain',
70+
'application/xml',
71+
'application/rss+xml',
72+
'text/html',
73+
'text/xml'
74+
];
75+
76+
const isTextContentType = (contentType: string): boolean => {
77+
let result = false;
78+
for (let i = 0; i < textTypes.length; i++) {
79+
if (contentType.toLowerCase().indexOf(textTypes[i]) >= 0) {
80+
result = true;
81+
break;
82+
}
83+
}
84+
return result;
85+
};
86+
6887
const requestCallbacks = new Map();
6988
let requestIdCounter = 0;
7089

@@ -146,12 +165,35 @@ export class Http {
146165
},
147166
onComplete(result: any): void {
148167
let content;
168+
let responseText;
149169
let isString = false;
150170
if (result.content instanceof org.json.JSONObject || result.content instanceof org.json.JSONArray) {
151171
content = deserialize(result.content);
172+
try {
173+
responseText = JSON.stringify(content);
174+
} catch (err) {
175+
this._reject({
176+
type: HttpError.Error,
177+
ios: null,
178+
message: err
179+
});
180+
return;
181+
}
152182
isString = true;
153183
} else {
154184
content = result.content;
185+
if (content instanceof java.lang.String || typeof content === 'string') {
186+
try {
187+
responseText = JSON.stringify(content);
188+
} catch (err) {
189+
this._reject({
190+
type: HttpError.Error,
191+
ios: null,
192+
message: err
193+
});
194+
return;
195+
}
196+
}
155197
}
156198
if (result && result.headers) {
157199
const length = result.headers.size();
@@ -207,14 +249,27 @@ export class Http {
207249
url: result.url,
208250
statusCode,
209251
headers,
210-
responseAsString: isString ? result.content.toString(): null,
252+
responseAsString: isString ? result.content.toString() : null,
211253
responseAsImage: null // TODO needs base64 Image
212254
}, headers);
213255
}
214256

257+
if (isTextContentType(returnType) && !responseText) {
258+
try {
259+
responseText = JSON.stringify(content);
260+
} catch (err) {
261+
this._reject({
262+
type: HttpError.Error,
263+
ios: null,
264+
message: err
265+
});
266+
return;
267+
}
268+
}
215269
resolve({
216270
url: result.url,
217271
content,
272+
responseText,
218273
statusCode: statusCode,
219274
headers: headers
220275
});

0 commit comments

Comments
 (0)