当前位置: 首页 > 知识库问答 >
问题:

NativeScript不允许到localhost的角度明文HTTP通信

楚勇
2023-03-14
 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 = "m@m.com";
     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));
 }
}
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位于新区域AwareError(文件:///data/data/org.nativescript.preview/files/app/Tns_modules/@nativescript/Angular/zone-js/dist/zone-nativescript.js:1298:31)\n位于onRequestComplete(文件:

试图通过遵循帖子中的建议来解决这个问题:Android8:明文HTTP流量不允许,并遵循该帖子中的建议。我在路径/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>

我还向路径res/xml/network_security_config.xml添加了堆栈溢出post建议的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以修复错误,但这并没有解决问题。

共有1个答案

孙恩
2023-03-14

请使用:

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

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

从模拟器中,localhost或127.0.0.1引用的是模拟器本身,而不是您的本地计算机。您需要使用ip 10.0.2.2,它桥接到您的本地计算机。

 类似资料: