javascript - React-Native:Android 上的“TypeError:网络请求失败”
问题描述
我的 Android 应用程序上的这个错误让我发疯了。
我正在我的应用程序中进行注册,并将 fetch 方法用于有效的 API。(我尝试使用 CocoaRestClient 插入数据并且它有效!)
我已经使用 fecth 完成了登录,它运行良好,但问题在于注册方法。
所以:
Registrazione.js
class Registrazione extends Component {
constructor(props) {
super(props);
this.state = {
FirstName: this.props.hasOwnProperty('FirstName') ? this.props.FirstName : '',
LastName: this.props.hasOwnProperty('LastName') ? this.props.LastName : '',
FiscalCode: this.props.hasOwnProperty('FiscalCode') ? this.props.FiscalCode : '',
identity: this.props.hasOwnProperty('identity') ? this.props.identity : '',
Email: this.props.hasOwnProperty('Email') ? this.props.Email: ''
}
}
registrazione () {
let ctrl = true;
if(ctrl){
if(global.isSafe){
Actions.sceglipassword({
Email: this.state.Email,
FirstName: this.state.FirstName,
LastName: this.state.LastName,
FiscalCode: this.state.FiscalCode
})
}
else{
Actions.creazioneutente({identity: this.state.identity, FirstName: this.state.FirstName, LastName: this.state.LastName, FiscalCode: this.state.FiscalCode, Email: this.state.Email})
}
}
};
然后我调用ScegliPassword页面:
class ScegliPassword extends Component {
constructor(props){
super(props);
this.state = {
Password: '',
PasswordRepeat: ''
};
}
registrazione(){
let ctrl = true;
if(
utility.isEmpty(this.state.Password) || utility.isEmpty(this.state.PasswordRepeat)){
ctrl = false;
Alert.alert("Error");
}
else if(this.state.Password != this.state.PasswordRepeat){
ctrl = false;
Alert.alert("Error");
}
if(ctrl)
{
let identity = {
"APPNAME":
{
Username: this.props.FiscalCode,
Password: this.state.Password
}
}
Actions.creazioneutente({identity: identity, FirstName: this.props.FirstName, LastName: this.props.LastName, FiscalCode: this.props.FiscalCode, Email: this.props.Email})
}
}
然后有一个动作到CreazioneUtente
class CreazioneUtente extends Component {
constructor(props)
{
super(props)
}
creaUtenza(){
Utente.creaUtente(this.props.identity, this.props.FirstName, this.props.LastName, this.props.FiscalCode, this.props.Email)
.then((data) => {
global.utente = new Utente(data)
})
.catch((err) => {
console.log(err)
})
}
我把Utente称为 .creaUtente
static creaUtente(identity, FirstName, LastName, FiscalCode , Email) {
let formdata = new FormData();
formdata.append('Identity', identity);
formdata.append('FirstName', FirstName);
formdata.append('LastName', LastName);
formdata.append('FiscalCode', FiscalCode);
formdata.append('Email', Email);
console.log(Configuration.base_url.rest + Configuration.apiRoutes.signUp)
console.log(formdata)
return new Promise((resolve, reject)=> {
fetch(Configuration.base_url.rest + Configuration.apiRoutes.signUp , {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body: formdata
})
.then((response) => response.json())
.then((responseData) => {
if(responseData.Error){
Alert.alert("Errore");
}
global.utente = responseData;
resolve(responseData)
})
.catch((err) => {reject(err)})
})
}
在 fecth 我传递了用于注册的 API 链接(它是 https://***.com 链接)
现在,如果我在控制台日志中打印“formdata”,我会收到随表单插入的正确数据。
这是我的 AndroidManifest.xml
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme"
xmlns:tools="http://schemas.android.com/tools"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning">
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
</application>
当我尝试进行注册时,我收到了这种错误(在我的控制台中):
{ _parts:
[ [ 'Identity',
{ "APPNAME": { Username: 'TNNFRZ77H01G273V', Password: '12345' } } ],
[ 'FirstName', 'Fabrizio' ],
[ 'LastName', 'Tonno' ],
[ 'FiscalCode', 'TNNFRZ77H01G273V' ],
[ 'Email', 'Fabrizio.tonno@email.com' ] ] }
{ [TypeError: Network request failed]
line: 24122,
column: 31,
sourceURL: 'http://localhost:8081/index.bundle?platform=android&dev=true&minify=false' }
我不知道如何解决这个问题......你能帮帮我吗?谢谢你。
解决方案
推荐阅读
- javascript - 如何使用三元运算符使用 React 渲染 jsx?
- spring-boot - Spring Security 自定义 PermissionEvaluator
- javascript - 根据反应编辑机器人的消息
- javascript - 如何使用 underscore.js 在列表元素中过滤具有匹配参数的对象?
- github - Github 存储库文件访问
- python - discord.py bot 正确构建在 heroku 上,但显示脱机
- arrays - SWIFT:我无法解析来自 URL 的数据?
- swiftui - 参数 SwiftUI 中的可选绑定
- reactjs - 在reactjs中使用令牌登录时检查用户是否登录的最佳方法是什么
- c - 如何将包含十六进制数字的字符串转换为c中的int