File tree Expand file tree Collapse file tree 2 files changed +86
-2
lines changed Expand file tree Collapse file tree 2 files changed +86
-2
lines changed Original file line number Diff line number Diff line change @@ -302,6 +302,53 @@ class FormUploader extends React.Component {
302
302
}
303
303
}
304
304
305
+ class FetchTest extends React . Component {
306
+
307
+ constructor ( props ) {
308
+ super ( props ) ;
309
+ this . state = {
310
+ responseText : null ,
311
+ } ;
312
+ }
313
+
314
+ submit ( uri : String ) {
315
+ fetch ( uri ) . then ( ( response ) => {
316
+ return response . text ( ) ;
317
+ } ) . then ( ( body ) => {
318
+ this . setState ( { responseText : body } ) ;
319
+ } ) ;
320
+ }
321
+
322
+ render ( ) {
323
+
324
+ var response = this . state . responseText ? (
325
+ < View style = { { marginTop : 10 } } >
326
+ < Text style = { styles . label } > Server response:</ Text >
327
+ < TextInput
328
+ editable = { false }
329
+ multiline = { true }
330
+ defaultValue = { this . state . responseText }
331
+ style = { styles . textOutput }
332
+ />
333
+ </ View >
334
+ ) : null ;
335
+
336
+ return (
337
+ < View >
338
+ < Text style = { styles . label } > Edit URL to submit:</ Text >
339
+ < TextInput
340
+ returnKeyType = "go"
341
+ defaultValue = "http://www.posttestserver.com/post.php"
342
+ onSubmitEditing = { ( event ) => {
343
+ this . submit ( event . nativeEvent . text ) ;
344
+ } }
345
+ style = { styles . textInput }
346
+ />
347
+ { response }
348
+ </ View >
349
+ ) ;
350
+ }
351
+ }
305
352
306
353
exports . framework = 'React' ;
307
354
exports . title = 'XMLHttpRequest' ;
@@ -316,6 +363,11 @@ exports.examples = [{
316
363
render ( ) {
317
364
return < FormUploader /> ;
318
365
}
366
+ } , {
367
+ title : 'fetch test' ,
368
+ render ( ) {
369
+ return < FetchTest /> ;
370
+ }
319
371
} ] ;
320
372
321
373
var styles = StyleSheet . create ( {
@@ -373,4 +425,19 @@ var styles = StyleSheet.create({
373
425
fontSize : 16 ,
374
426
fontWeight : '500' ,
375
427
} ,
428
+ label : {
429
+ flex : 1 ,
430
+ color : '#aaa' ,
431
+ fontWeight : '500' ,
432
+ height : 20 ,
433
+ } ,
434
+ textOutput : {
435
+ flex : 1 ,
436
+ fontSize : 17 ,
437
+ borderRadius : 3 ,
438
+ borderColor : 'grey' ,
439
+ borderWidth : 1 ,
440
+ height : 200 ,
441
+ paddingLeft : 8 ,
442
+ } ,
376
443
} ) ;
Original file line number Diff line number Diff line change @@ -271,10 +271,27 @@ - (void)sendData:(NSData *)data forTask:(RCTDownloadTask *)task
271
271
encoding = CFStringConvertEncodingToNSStringEncoding (cfEncoding);
272
272
}
273
273
274
+ // Attempt to decode text
274
275
NSString *responseText = [[NSString alloc ] initWithData: data encoding: encoding];
275
276
if (!responseText && data.length ) {
276
- RCTLogWarn (@" Received data was invalid." );
277
- return ;
277
+
278
+ // We don't have an encoding, or the encoding is incorrect, so now we
279
+ // try to guess (unfortunately, this feature is available of iOS 8+ only)
280
+ if ([NSString respondsToSelector: @selector (stringEncodingForData:
281
+ encodingOptions:
282
+ convertedString:
283
+ usedLossyConversion: )]) {
284
+ [NSString stringEncodingForData: data
285
+ encodingOptions: nil
286
+ convertedString: &responseText
287
+ usedLossyConversion: NULL ];
288
+ }
289
+
290
+ // If we still can't decode it, bail out
291
+ if (!responseText) {
292
+ RCTLogWarn (@" Received data was not a string, or was not a recognised encoding." );
293
+ return ;
294
+ }
278
295
}
279
296
280
297
NSArray *responseJSON = @[task.requestID, responseText ?: @" " ];
You can’t perform that action at this time.
0 commit comments