Added logic to detect text encoding for downloaded data

Summary: @​public

RCTNetworking currently relies on network responses to include an accurate text encoding, otherwise it is unable to convert the response data to text unless it's encoded as UTF8.

See: https://github.com/facebook/react-native/issues/1780#issuecomment-139334294 for details.

This diff makes use of a new feature in iOS8 to detect the encoding of the text authomatically

Reviewed By: @sahrens

Differential Revision: D2443446
This commit is contained in:
Nick Lockwood
2015-09-16 07:40:14 -07:00
committed by facebook-github-bot-6
parent 890c2193d0
commit 0044b3c2c3
2 changed files with 86 additions and 2 deletions

View File

@@ -302,6 +302,53 @@ class FormUploader extends React.Component {
}
}
class FetchTest extends React.Component {
constructor(props) {
super(props);
this.state = {
responseText: null,
};
}
submit(uri: String) {
fetch(uri).then((response) => {
return response.text();
}).then((body) => {
this.setState({responseText: body});
});
}
render() {
var response = this.state.responseText ? (
<View style={{marginTop: 10}}>
<Text style={styles.label}>Server response:</Text>
<TextInput
editable={false}
multiline={true}
defaultValue={this.state.responseText}
style={styles.textOutput}
/>
</View>
) : null;
return (
<View>
<Text style={styles.label}>Edit URL to submit:</Text>
<TextInput
returnKeyType="go"
defaultValue="http://www.posttestserver.com/post.php"
onSubmitEditing={(event)=> {
this.submit(event.nativeEvent.text);
}}
style={styles.textInput}
/>
{response}
</View>
);
}
}
exports.framework = 'React';
exports.title = 'XMLHttpRequest';
@@ -316,6 +363,11 @@ exports.examples = [{
render() {
return <FormUploader/>;
}
}, {
title: 'fetch test',
render() {
return <FetchTest/>;
}
}];
var styles = StyleSheet.create({
@@ -373,4 +425,19 @@ var styles = StyleSheet.create({
fontSize: 16,
fontWeight: '500',
},
label: {
flex: 1,
color: '#aaa',
fontWeight: '500',
height: 20,
},
textOutput: {
flex: 1,
fontSize: 17,
borderRadius: 3,
borderColor: 'grey',
borderWidth: 1,
height: 200,
paddingLeft: 8,
},
});