This error generally happend when trying to connect the react native application to locahost wamp server.
To view the actual cause of this error we have to change the response.json() with response.text()
It looks like your response might be malformed (or empty string).
1. Try replacing response.json() with response.text() and compare the raw response on Android and iOS.
2. make sure that your "my-link" is a valid URL
3. Check the response.ok and response.status - maybe request just failed?
4. Check if you have internet access on Android device/emulator (e.g. open that URL in a browser)
But in my problem the point 1st worked.
This is my react native code app.js
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, TextInput, View, Alert, Button, Text } from 'react-native';
export default class MainProject extends Component {
constructor(props) {
super(props)
this.state = {
UserName: '',
UserEmail: '',
UserPassword: ''
}
}
UserRegistrationFunction = () =>{
const { UserName } = this.state ;
const { UserEmail } = this.state ;
const { UserPassword } = this.state ;
const baseUrl = Platform.OS === 'android' ? 'http://10.0.2.2' : 'http://localhost';
fetch('http://10.0.2.2/test/user_registration.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: UserName,
email: UserEmail,
password: UserPassword
})
}).then((response) => response.text())
.then((responseJson) => {
// Showing response message coming from server after inserting records.
Alert.alert(responseJson);
}).catch((error) => {
console.error(error);
});
}
render() {
return (
<View style={styles.MainContainer}>
<Text style= {{ fontSize: 20, color: "#000", textAlign: 'center', marginBottom: 15 }}>User Registration Form</Text>
<TextInput
// Adding hint in Text Input using Place holder.
placeholder="Enter User Name"
onChangeText={UserName => this.setState({UserName})}
// Making the Under line Transparent.
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
/>
<TextInput
// Adding hint in Text Input using Place holder.
placeholder="Enter User Email"
onChangeText={UserEmail => this.setState({UserEmail})}
// Making the Under line Transparent.
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
/>
<TextInput
// Adding hint in Text Input using Place holder.
placeholder="Enter User Password"
onChangeText={UserPassword => this.setState({UserPassword})}
// Making the Under line Transparent.
underlineColorAndroid='transparent'
style={styles.TextInputStyleClass}
secureTextEntry={true}
/>
<Button title="Click Here To Register" onPress={this.UserRegistrationFunction} color="#2196F3" />
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
justifyContent: 'center',
flex:1,
margin: 10
},
TextInputStyleClass: {
textAlign: 'center',
marginBottom: 7,
height: 40,
borderWidth: 1,
// Set border Hex Color Code Here.
borderColor: '#2196F3',
// Set border Radius.
borderRadius: 5 ,
// Set border Radius.
//borderRadius: 10 ,
}
});
AppRegistry.registerComponent('MainProject', () => MainProject);and this is my php code
<?php
include('db.php');
$UserName = $decodedData['UserName'];
$UserEmail = $decodedData['UserEmail'];
$UserPassword = md5($decodedData['UserPassword']); //password is hashed
/*
$SQL = "SELECT * FROM student WHERE UserEmail = '$UserEmail'";
$exeSQL = mysqli_query($conn, $SQL);
$checkEmail = mysqli_num_rows($exeSQL);
if ($checkEmail != 0) {
$Message = "Already registered";
} else {
*/
$InsertQuerry = "INSERT INTO users1(UserName,UserEmail,UserPassword) VALUES('$UserName', '$UserEmail', '$UserPassword')";
$R = mysqli_query($conn, $InsertQuerry);
if ($R) {
$Message = "Complete--!";
} else {
$Message = "Error";
}
//}
$response[] = array("Message" => $Message);
echo json_encode($response);
?>
Comments
Post a Comment