获取请求在iOS上显示响应,但不使用android

获取请求在iOS上显示响应,但不使用android,android,node.js,react-native,express,android-manifest,Android,Node.js,React Native,Express,Android Manifest,我正在尝试使用React Native(客户端)和Node/Express(服务器端)从获取请求发送响应。不幸的是,该值仅在iOS emulator中显示,而在android中不显示。我在github上看到一篇文章,建议在AndroidManifest文件上添加一些代码,但它仍然不起作用,我尝试了API 25到29的设备,但没有成功。有人能帮忙吗?谢谢你抽出时间 前端 import React, { Component } from "react"; import {Modal, StyleSh

我正在尝试使用React Native(客户端)和Node/Express(服务器端)从获取请求发送响应。不幸的是,该值仅在iOS emulator中显示,而在android中不显示。我在github上看到一篇文章,建议在AndroidManifest文件上添加一些代码,但它仍然不起作用,我尝试了API 25到29的设备,但没有成功。有人能帮忙吗?谢谢你抽出时间

前端

import React, { Component } from "react";
import {Modal, StyleSheet, View, StatusBar, Text} from 'react-native'
import { Provider } from 'mobx-react';
import StateStorage from './StateStorage';


export default class App extends React.Component {

  constructor (props) {
    super(props)
    this.state ={

      data:''

    }
  }
  componentDidMount(){
    this.callBackendAPI()
    .then(res => this.setState({ data: res.express }))
    .catch(err => console.log(err));
  }
  callBackendAPI = async () => {
    const response = await fetch('http://localhost:5000');
    const body = await response.json();

    if (response.status !== 200) {
      throw Error(body.message) 
    }
    return body;
  };
  render() {
    return (
      <Provider store={StateStorage}>

<Text style={{top:100}}>{this.state.data}</Text>
        </Provider >
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',

  },

});
AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
  package="com.mapp">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:allowBackup="false"
      android:usesCleartextTraffic="true"
      tools:targetApi="28"
      android:theme="@style/AppTheme">
      <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" />
       <meta-data
     android:name="com.google.android.geo.API_KEY"
     android:value="AIzaSyD4Js3-PNHs1aL4XEmw6mAomblC2b4ys5s"/>
    </application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

我在这里看到两个问题:

  • 从API 28开始,默认情况下会阻止未加密的HTTP请求。
    如果要启用HTTPS,请切换到HTTPS或查看

  • android emulator在一个虚拟路由器后面运行,该路由器将其与开发机器网络隔离,如果您想访问它,必须使用
    10.0.2.2
    ,而不是
    127.0.0.1
    (或
    localhost
    )。
    有关更多详细信息,请参阅文档


  • 为了分享更多关于@bug-answer的信息,可以采取以下步骤,并且@bug解释也适用于这些步骤

    创建一个xml文件,您可以调用网络安全配置.xml或任何东西。 网络安全.xml的内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">10.0.2.2</domain>
            <domain includeSubdomains="true">https://your_api_url_ssh_support</domain>
            <domain includeSubdomains="true">http://your_api_url</domain>
        </domain-config>
    </network-security-config>
    
    
    10.0.2.2
    https://your_api_url_ssh_support
    http://your_api_url
    
    示例real network_security_config.xml如下所示:

    
    10.0.2.2
    https://18.222.76.221
    http://18.222.76.221
    
    然后您的清单文件被精简到与此问题相关的内容:

    
    
    注意:android:networkSecurityConfig=“@xml/network\u security\u config”

    还要注意,要访问android模拟器的真实地址,我们必须指向@bug解释的10.0.2.2

    我是否忘了提到network_security_config.xml将放在xml文件夹中的res目录中。 所以,它看起来是这样的:res->xml->network\u security\u config.xml 快乐编码


    关于@camilebasbous的呢,但是当我尝试时它不起作用,这就是这里的问题。所以这个问题还没有解决。我@camilebasbous,很抱歉回来晚了。我注意到另一个潜在的问题,基本上你不能使用localhost从Android模拟器访问你的开发机器。请查看我的更新答案以了解更多详细信息。@bug谢谢你的回复,我会在我回到这个项目时检查它,atm我还有我的关注点,请确认问题是否已通过@bug的回答得到解决。如果你已经在本地机上部署了服务器,你应该与服务器在同一个网络上。同时分享你的android代码,了解你在设备上得到了什么。
    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">10.0.2.2</domain>
            <domain includeSubdomains="true">https://your_api_url_ssh_support</domain>
            <domain includeSubdomains="true">http://your_api_url</domain>
        </domain-config>
    </network-security-config>
    
    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <domain-config cleartextTrafficPermitted="true">
            <domain includeSubdomains="true">10.0.2.2</domain>
            <domain includeSubdomains="true">https://18.222.76.221</domain>
            <domain includeSubdomains="true">http://18.222.76.221</domain>
        </domain-config>
    </network-security-config>
    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
      package="com.mapp">
    
        <uses-permission android:name="android.permission.INTERNET" />
    
        <application
          android:name=".MainApplication"
          android:label="@string/app_name"
          android:icon="@mipmap/ic_launcher"
          android:roundIcon="@mipmap/ic_launcher_round"
          android:allowBackup="false"
          android:networkSecurityConfig="@xml/network_security_config"
          android:usesCleartextTraffic="true"
          tools:targetApi="28"
          android:theme="@style/AppTheme">
     
        </application>