-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerExample.js
More file actions
85 lines (75 loc) · 2.61 KB
/
ServerExample.js
File metadata and controls
85 lines (75 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React, {Component} from 'react'
import { Text, StyleSheet, View, Button, Alert, TextInput} from 'react-native'
import App from './App'
export default class ServerExample extends Component {
constructor(props) {
super(props)
this.state = {
response: "click to connect to server",
name: "ddf"
}
}
connect = async () => {
const URL = "http://192.249.18.167:443/welcome"
try {
//const response = await fetch(URL + "/" + this.state.name);
const user = await fetch(URL, {
method: "POST",
body: JSON.stringify({name: this.state.name}),
headers: {
"Content-Type": "application/json"
}
})
if(response.status != 200){
throw new Error("Something is wrong"+response.status + this.state.name)
}
const response = await fetch(URL, {
method: "POST",
body: JSON.stringify({name: this.state.name}),
headers: {
"Content-Type": "application/json"
}
})
if(response.status != 200){
throw new Error("Something is wrong"+response.status + this.state.name)
}
const responseText = await response.text();
this.setState({response: responseText});
}catch(error) {
Alert.alert(error.message);
}
// fetch(URL).then(response => {
// if(response.status == 200) {
// return response.text()
// }
// else {
// throw new Error("Something is wrong")
// }
// }).then(resonseText => {
// this.setState({response: resonseText});
// }).catch(error => {
// console.error(error.message)
// });
}
setText = (text) => {
//throw new Error("Something is wrong"+text)
this.setState({name: text});
}
render() {
return (
<View style={styles.title}>
<TextInput placeholder ="your name" onChangeText={this.setText}/>
<Text style={styles.title}> {this.state.response} </Text>
<Button title="connect" style={styles.title} onPress={this.connect}></Button>
</View>
)
}
}
const styles = StyleSheet.create({
title: {
fontSize: 20,
marginTop: 100,
margin: 10,
textAlign: 'center'
}
})