当我使用react-native-xml2js API发送带有抓取的https请求时,按钮事件将被阻止,直到请求完成为止。如何在不阻止事件的情况下使用新任务来使用xml2js?我必须等待网络服务结束才能单击按钮
我的代码:
const parseString = require('react-native-xml2js').parseString;
var base64 = require('base-64');
const headers = new Headers();
headers.append('Content-Type', 'text/xml');
headers.append('SOAPAction', 'basicInvoke');
headers.append('Authorization', 'Basic ' + base64.encode("user:pass"));
sr =
'<?xml version="1.0" encoding="utf-8"?>' +
'<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"'+
' xmlns:get="http://xmlns.oracle.com/orawsv/ABL/WSS_MWEB_CLI/GETARTILE">'+
' <soapenv:Header/>'+
' <soapenv:Body>'+
'<get:CGETARTICLE_OUT-GETARTICLEInput>'+
'<get:SQLMSGIN-GETARTICLE_IN-CIN>'+
' <get:GETARTICLE_IN>'+
' <get:STE>'+global.ste+'</get:STE>'+
' <get:CLI>'+global.cli+'</get:CLI>'+
' <get:DTLIV></get:DTLIV>'+
' <get:FAM></get:FAM>'+
' <get:PROMO>N</get:PROMO>'+
' <get:TRI>LIB</get:TRI>'+
' <get:NUMPAGE>0</get:NUMPAGE>'+
' <get:TAILLEPAGE>0</get:TAILLEPAGE>'+
' </get:GETARTICLE_IN>'+
'</get:SQLMSGIN-GETARTICLE_IN-CIN>'+
'</get:CGETARTICLE_OUT-GETARTICLEInput>'+
'</soapenv:Body>' +
'</soapenv:Envelope>';fetch('https://xxx.xxx.xx.xxx:1443/orawsv/ABL/WSS_MWEB_CLI/GETARTICLE', {
method: 'POST',
headers: headers,
body: sr,
})
.then(response => response.text() )
.then(bodyText => {
new Promise( function (resolve, reject){
parseString(bodyText, function (err, resultBody) {
var soap=resultBody["soap:Envelope"]
var result=soap["soap:Body"][0].GETARTICLEOutput[0].RETURN[0]
if(result.GETARTICLE_OUT[0].ERREUR_LIST[0]!== '')
{
Alert.alert(
'Erreur connexion',
result.GETARTICLE_OUT[0].ERREUR_LIST[0].SQLERREUR[0].ERR_MSG[0] ,
[
{text: 'OK', onPress: () => console.log('OK Pressed')},
],
{ cancelable: false }
)
}
else {
const db = SQLite.openDatabase('bdd.db', '1.0', 'SQLite Database', 1);
db.transaction(function (txn) {
var product=[];
for(var i=0; i<Object.keys(result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE).length ;i++ )
{
product= [
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].LIB1[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].LIB2[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].TYP[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].VOL[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].POIDS[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].FERM[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].FAM[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].SFAM[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].TG[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].PRV[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].PRLK[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CDTV[0],
'10',// result.RETURN.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].NBU[0],
'BOUT',//result.RETURN.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CDU[0],
'FUT',//result.RETURN.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CDC[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].PROMO[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].NOUV[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].IMG[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CODE[0]
];
txn.executeSql('INSERT OR IGNORE INTO products (lib1,lib2,typ,vol,poids,ferm,fam,sfam,tg,prv,prlk,cdtv,nbu,cdu,cdc,promo,nouv,img,code) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)', product );
txn.executeSql('UPDATE products SET lib1=?,lib2=?,typ=?,vol=?,poids=?,ferm=?,fam=?,sfam=?,tg=?,prv=?,prlk=?,cdtv=?,nbu=?,cdu=?,cdc=?,promo=?,nouv=?,img=? WHERE code=? ', product);
var packing=[]
txn.executeSql('DELETE FROM packing WHERE code_pro=?',result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CODE[0]);
for(var j=0; j<Object.keys(result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CDT_LIST[0].GETARTICLE_CDT).length ;j++ )
{
packing=[
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CDT_LIST[0].GETARTICLE_CDT[j].CDT[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CDT_LIST[0].GETARTICLE_CDT[j].CODE[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CDT_LIST[0].GETARTICLE_CDT[j].CDE[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CDT_LIST[0].GETARTICLE_CDT[j].NBU[0],
result.GETARTICLE_OUT[0].ART_LIST[0].GETARTICLE_ARTICLE[i].CODE[0]
]
txn.executeSql('INSERT INTO packing (cdt,code,cde,nbu,code_pro) VALUES (?,?,?,?,?)', packing );
}
}
}.bind(this));
}
console.log(result)
this.setState({load: 'Loaded'});
this._loadProducts();
}.bind(this));
}.bind(this))
.catch((error) => {
this.setState({load: "Echec"})
console.error(error);
})
})
.catch((error) => {
this.setState({load: "Echec"})
console.error(error);
});
你能帮我吗?