android - 不允许 NativeScript Angular 明文 HTTP 流量到本地主机

标签 android angular xml typescript nativescript-angular

我正在使用 native 脚本的 Angular 版本,并尝试创建一个到我的网络的登录系统。
下面是我尝试登录网络的代码。

home.component.ts

 import { LoginService } from '../Services/login/login.service';
 export class LoginComponent implements OnInit {

 public user: User;
 loginForm: FormGroup;

 constructor(private router: Router,
     private userService: UserService,
     private page: Page,
     public  loginDetails: LoginDetails,
     private LoginService: LoginService

 ) {

     this.user = new User();
     this.user.email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4c210c21622f2321" rel="noreferrer noopener nofollow">[email protected]</a>";
     this.user.password = "pass123";
     this.user.logo = "~/Images/opos_logo.png";
 }

 submit() {

    /* set a user variable with data from the formGroup */
    const user: string = this.user.email;
    /* set a password variable with data from the formGroup */
    const password: string = this.user.password;

    this.LoginService.loginUser(user, password)
        .subscribe(data => this.checkUser(data));
 }
}

服务/登录/login.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from "@angular/core";
import { Jwt } from '../../Classes/jwt';


@Injectable({
    providedIn: 'root'
})
export class LoginService {


  loginUrl = 'http://localhost/Login.w';

  constructor(private _http: HttpClient) { }

  loginUser(user, pass) {
      return this._http.get<Jwt>(
          this.loginUrl, { params: { fi_user: user, fi_pass: pass } });
  }
}

当我尝试像这样运行我的应用程序时,我会收到以下错误:

“originalStack”:“错误:java.io.IOException:不允许到本地主机的明文HTTP流量\n在新的ZoneAwareError(file:///data/data/org.nativescript.preview/files/app/tns_modules/@nativescript/angular/zone-js/dist/zone-nativescript.js:1298:31)\n 在 onRequestComplete (file:///data/data/org.nativescript.preview/files/app/tns_modules/@nativescript/core/http/http-request/http-request.js:54:30)\n 在 Object.onComplete (file:///data/data/org.nativescript.preview/files/app/tns_modules/@nativescript/core/http/http-request/http-request.js:43:7)"

尝试按照帖子 Android 8: Cleartext HTTP traffic not permitted 上的建议解决问题并遵循了该帖子的建议。 我在路径/App_Resources/Android/AndroidManifest.xml 上向我的程序添加了一个 androidmanifest.xml 文件(这还不是应用程序的一部分,因此我不确定应用程序是否正在访问该文件)。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="__PACKAGE__"
android:versionCode="1"
android:versionName="1.0">

<supports-screens
    android:smallScreens="true"
    android:normalScreens="true"
    android:largeScreens="true"
    android:xlargeScreens="true"/>

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="__APILEVEL__"/>

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

<application
    android:name="com.tns.NativeScriptApplication"
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:networkSecurityConfig="@xml/network_security_config">

    <activity
        android:name="com.tns.NativeScriptActivity"
        android:label="@string/title_activity_kimera"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:theme="@style/LaunchScreenTheme">

        <meta-data android:name="SET_THEME_ON_LAUNCH" android:resource="@style/AppTheme" />

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.tns.ErrorReportActivity"/>
</application>

我还添加了一个 network_security_config.xml 文件,堆栈溢出帖子建议将其添加到路径 res/xml/network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
   <domain-config cleartextTrafficPermitted="true">
      <domain includeSubdomains="true">domain.com (to be adjusted)</domain>
   </domain-config>
</network-security-config>

我还尝试将应用程序 android:usesCleartextTraffic="true"标记添加到我的 home.component.html 中以修复错误,但这并没有解决问题。

最佳答案

尝试:

loginUrl = 'http://10.0.2.2/Login.w';

我刚刚读过文章:https://stackoverflow.com/a/48591525 @EdwardGarson 写道:

From the emulator, localhost or 127.0.0.1 refers to the emulator itself, not your local machine. You need to use ip 10.0.2.2, which is bridged to your local machine.

问候

关于android - 不允许 NativeScript Angular 明文 HTTP 流量到本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685073/

相关文章:

typescript - 使用 Visual Studio Code 调试 webpack 捆绑的节点 ts

javascript - 如何以 Angular 创建和触发自定义事件

Angular 2 : pass a value to the route data resolve

java - 为什么 Android Studio 总是在应用程序设计中显示 ActionBar,即使在禁用时也是如此?

android - 如何将线性布局居中到其父级的中间?

android - 将 gradle 依赖项添加到库 aar 包

android - getIntent() 方法未为类型 ParseApplication 定义

Android:如何使用 shell 命令或 Intent 运行 google music 的 "Shuffle all"?

java - 错误 : java. lang.String 无法转换为 org.w3c.dom.Node

xml - 使用 BaseX 读取多个 XML 文件