我正在尝试通过获取api上传图像,但在真实设备android上出现网络请求失败错误。我也尝试了很多来自Google的建议,但对我没有任何帮助。
我的依赖项是:
"react-native": "0.62.0",
"axios": "^0.19.2",
"form-data": "^3.0.0",
"react": "16.11.0",
"react-redux": "^7.2.0",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
我上传图片的片段:
const imagePick = () => {
const formData = new FormData();
try {
const options = {
title: 'Select Avatar',
storageOptions: {
skipBackup: true,
path: 'images',
},
};
ImagePicker.showImagePicker(options, (response) => {
formData.append('avatar', {
uri: response.uri,
type: response.type,
name: response.fileName,
})
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${authToken}`
},
body: formData
})
.then(res => {
console.log(res.status)
})
.catch(e => {
console.log(e)
})
});
} catch (e) {
toast("Unable to upload profile photo")
}
}
我也在使用安全的https网址;
我也遇到过同样的问题,但是我想这个问题与axios库无关,而是因为React Native本身。
如此处提到的comment,是由于Flipper。
因此,直到React Native发挥作用之前,您都可以在MainApplication.java的下面提到的行中进行注释
initializeFlipper(this,getReactNativeHost()。getReactInstanceManager());
要发表评论,将// //放在上一行的前面
// initializeFlipper(this,getReactNativeHost()。getReactInstanceManager());