【问题标题】:Android app giving network error when connecting to a databaseAndroid 应用程序在连接到数据库时出现网络错误
【发布时间】:2021-07-09 16:11:10
【问题描述】:

我有一个 react native 应用程序,它从数据库中获取书籍并显示它们。当 mongodb 后端运行时,它在 iOS 上运行良好,但在 android 上却不行。我只是收到如下所示的网络错误。

你们中有人知道如何解决吗? 这是我的 build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "29.0.2"
        minSdkVersion = 16
        compileSdkVersion = 29
        targetSdkVersion = 29
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.5.3")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url("$rootDir/../node_modules/react-native/android")
        }
        maven {
            // Android JSC is installed from npm
            url("$rootDir/../node_modules/jsc-android/dist")
        }

        google()
        jcenter()
        maven { url 'https://www.jitpack.io' }
    }
}

这是我的 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />s
    <uses-sdk android:targetSdkVersion="29" android:minSdkVersion="16" tools:overrideLibrary="com.facebook.react"/>
    <application android:usesCleartextTraffic="true" tools:targetApi="28" tools:ignore="GoogleAppIndexingWarning" />
</manifest>

请告诉我如何解决此问题。 谢谢。

(如果您需要查看任何前端或后端文件,请在 cmets 中告诉我。)

【问题讨论】:

    标签: android database mongodb react-native android-studio


    【解决方案1】:

    我解决了这个问题,并认为该解决方案可能对其他人有所帮助。所发生的一切是模拟器没有获得本地主机。为了解决这个问题,我在前端将 localhost:3000 替换为 10.0.2.2:3000。它解决了这个问题。

    代替

    .post('http://localhost:3000/login'...
    

    应该是

    .post('http://10.0.2.2:3000/login'...
    

    感谢@Raghav Herugu 帮助我解决问题

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 2017-12-25
      • 2017-05-09
      • 1970-01-01
      相关资源
      最近更新 更多