![](/img/trans.png)
[英]react native android: TypeError: Network request failed
[英]React Native 0.66.* axios call [TypeError: Network request failed] ANDROID
嗨,我是 React Native 的新手
我有 macbook,我正在使用 Laravel valet park 在 macbook 中運行 Laravel 項目
很簡單,我正在嘗試使用在 laravel 8 中開發的任何 API 來生成 Auth 令牌。當我使用郵遞員點擊 API 時,我得到了 100% 的響應。 但是當我嘗試通過在 react native 中使用 axios 來訪問這個 API 時,我總是收到“NETWROK ERROR”作為響應。
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]);
})
}
}
總是收到“網絡錯誤”作為回應。
注意:我已經通過以下 linse 更新了 AndroidManifest.xml 文件
<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" />
但是沒有任何變化,仍然收到“網絡錯誤”,命中 API
有時,當您的 wifi 網絡更改或重新連接時,android 模擬器會遇到網絡問題,重新運行您的模擬器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.