在React-native上使用Axios发行错误网络

时间:2020-11-04 15:36:43

标签: react-native axios

我在Axios POST方法上遇到了“网络错误”问题。我的api没有问题,我在一个新项目中尝试了此代码,并且效果很好,我还尝试在随机api中使用相同的代码发布我当前的项目也可以。我的最后一个想法是问题出在我项目中。因此,如果您对此有所了解。.非常感谢您的回答。

这是我的包。JSON:

    
    {
    "name": "MyApp",
    "version": "0.0.1",
    "private": true,
    "scripts": {
      "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint . --ext .js,.jsx"
     },
     "dependencies": {
    "@eva-design/eva": "2.0.0",
    "@react-native-async-storage/async-storage": "^1.13.2",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/progress-bar-android": "^1.0.4",
    "@react-native-community/progress-view": "^1.2.3",
    "@react-navigation/native": "^5.8.4",
    "@react-navigation/stack": "^5.12.1",
    "@ui-kitten/components": "5.0.0",
    "@ui-kitten/eva-icons": "5.0.0",
    "axios": "^0.21.0",
    "react": "16.11.0",
    "react-native": "^0.62.2",
    "react-native-gesture-handler": "^1.8.0",
    "react-native-pdf": "^6.2.2",
    "react-native-safe-area-context": "^3.1.8",
    "react-native-screens": "^2.12.0",
    "react-native-share": "^4.0.4",
    "react-native-svg": "^12.1.0",
    "rn-fetch-blob": "^0.12.0"
     },
     "devDependencies": {
    "@babel/core": "^7.6.2",
    "@babel/runtime": "^7.12.5",
    "@react-native-community/eslint-config": "^1.0.0",
    "@ui-kitten/metro-config": "5.0.0",
    "babel-jest": "^24.9.0",
    "eslint": "^6.5.1",
    "jest": "^24.9.0",
    "metro-react-native-babel-preset": "^0.58.0",
    "react-test-renderer": "16.11.0"
    },
     "jest": {
    "preset": "react-native",
    "transformIgnorePatterns": []
     }
     }

最后是我的代码,但是正如我所说的,它适用于其他api或使用当前api的其他项目:

     import AsyncStorage from '@react-native-async-storage/async-storage';
     import axios from 'axios';$
 
     export default class Webservice {
    static baseUrl = 'https://extranet.mairie-albi.fr:11471/';
    static urlWebservice = this.baseUrl + 'authorisation.php';

    static async request(method, state) {
    const tokenMobile = await AsyncStorage.getItem('tokenMobile');
    const keyConnect = await AsyncStorage.getItem('keyConnect');
    const login = await AsyncStorage.getItem('idUser');

    return await axios({
      method: 'POST',
      url: this.urlWebservice,
      data: {
        tokenMobile: tokenMobile,
        keyConnect: keyConnect,
        methode: method,
        etat: state,
        login: login,
      },
    })
      .then((response) => {
          console.log(response)
        return response;
      })
      .catch((err) => {
        alert(err.toString());
        console.log( `première erreur ${err}`)
        return false;
      });
    }

    /**
     *
     * @param listeInit [username, password]
     * @returns {Promise<void>}
     */
     static async initDonneesAccueil(listeInit) {
      let username = listeInit[0];
      let password = listeInit[1];

    return await axios({
      method: 'POST',
      url: this.urlWebservice,
      data: {
        username: username,
        password: password,
        methode: 'initDonneesAccueil',
        etat: 'init',
      },
    })
      .then(response => {
          console.log(response)
        if (response.data.status) {
          AsyncStorage.setItem('tokenMobile', response.data.tokenmsg);
          AsyncStorage.setItem('keyConnect', response.data.keyconnect);
          AsyncStorage.setItem(
            'tokenTimestamp',
            (+new Date() + 3600000).toString(),
          );
          AsyncStorage.setItem(
            'initDonneesAccueil',
            JSON.stringify(response.data.resultat),
          );
          AsyncStorage.setItem('idUser', username);
          return true;
        } else {
          if (response.data.msgErreur) {
            alert(response.data.msgErreur);
          } else {
            alert(response.data);
          }
          return false;
        }
      })
      .catch(err => {
        alert(err.toString());
        console.log(`deuxième erreur ${err}`)
        return false;
      });
     }

     static async getLoginPwd(keyConnect: String): Array {}


     static async APIValidate(apiKey: Array): String {}

     static async KeyConnectValidate(apiKeyConnect: Array): String {}

     static async getTokenRequest(tokenKey: Array): String {}

     static async TokenValidate(token: Array): String {
      return this.request('TokenValidate', 'detail');
    }

    static async getTokenKeyConnect(tokenKeyConnect: Array): Array {}
    }

1 个答案:

答案 0 :(得分:0)

好吧,我终于解决了我的问题:就像@Nisharg Shah的链接中所说的那样,首先,在确保您的Flipper版本并将其更改为0.54.0之后,您需要拥有一个react-native版本0.63.3。 。如果像我一样,仅创建一个新项目就行不通,将安装最新版本。我的依赖项“ react native pdf”也有一个问题“使用操作系统独立路径'lib / x86 / libjsc.so'找到了多个文件”,将android / app / build.gradle文件添加到android对象中:

 packagingOptions {
    pickFirst 'lib/x86/libc++_shared.so'
    pickFirst 'lib/x86_64/libjsc.so'
    pickFirst 'lib/arm64-v8a/libjsc.so'
    pickFirst 'lib/arm64-v8a/libc++_shared.so'
    pickFirst 'lib/x86_64/libc++_shared.so'
    pickFirst 'lib/armeabi-v7a/libc++_shared.so'
}

一切都应该正常工作:)。