React Native 0.66.* axios call [TypeError: Network request failed] ANDROID

Viewed 289

Hi i am new in react native

i have macbook and i am running Laravel projects in macbook by using Laravel valet park

simple i am trying to generating Auth token by using any API which is developed in laravel 8. when i hit API by using postman then i got 100% response. but when i try to hit this API by using axios in react native always i am getting "NETWROK ERROR" in response.

 import axios from 'axios';
axios.defaults.baseURL = 'http://cms.test/';
const  LoginScreen = ({navigation}) => {
    const [email,setEmail] = useState('');
    const [password,setPassword] = useState('');
    const [user, setUser] = useState(null);
    const [error, setError] = useState(null);
    const login = async() => {
        if(!email || !password){
            Alert.alert('Please fill all fields')
        }else{
            axios.post('api/token', {
                email,
                password,
                device_name: 'mobile',
            }, {
            headers: {
                'Content-Type': 'application/json'
            }
                })
              .then(response => {
                const userResponse = {
                  email: response.data.user.email,
                  token: response.data.token,
                }
                setUser(userResponse);
                setError(null);
                // SecureStore.setItemAsync('user', JSON.stringify(userResponse));
                AsyncStorage.setItem('user', JSON.stringify(userResponse));
              })
              .catch(error => {
                  console.log(error);
                  return false;
                const key = Object.keys(error.response.data.errors)[0];
                setError(error.response.data.errors[key][0]);
              })
        }
    }

Always getting "NETWORK ERROR" in response.

Note: i already updated AndroidManifest.xml file by these below linse

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:usesCleartextTraffic="true" tools:targetApi="28" tools:ignore="GoogleAppIndexingWarning" />

But there is not any change, still getting "NETWORK ERROR", on hit API

1 Answers

Some times android emulator get problem with network when your wifi network changed or reconnected re-run your emulator again.

Related