【问题标题】:NativeScriptAngular http request error Android 4.2NativeScriptAngular http请求错误Android 4.2
【发布时间】:2020-06-21 06:40:55
【问题描述】:

我正在使用 Angular NativeScript 制作应用程序,我正在使用 Web 服务,但是当我尝试在 Android 4 中执行 http 请求时,它会引发此错误。但是当我在 Android 9 上执行此操作时,没有错误,并且可以插入。

app.module.ts

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule, NativeScriptHttpClientModule, NativeScriptFormsModule } from "@nativescript/angular";
import { NgShadowModule } from 'nativescript-ng-shadow';
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { LoginComponent } from './components/login/login.component';
import { HeaderComponent } from './components/partials/header/header.component';
import { FooterComponent } from './components/partials/footer/footer.component';
import { IndexComponent } from './components/index/index.component';
import { RegistrarComponent } from './components/registrar/registrar.component';


@NgModule({
    bootstrap: [
        AppComponent
    ],
    imports: [
        NativeScriptModule,
        AppRoutingModule,
        NativeScriptHttpClientModule,
        NgShadowModule,
        NativeScriptFormsModule
    ],
    declarations: [
        AppComponent,
        LoginComponent,
        HeaderComponent,
        FooterComponent,
        IndexComponent,
        RegistrarComponent,
    ],
    providers: [],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
/*
Pass your application module to the bootstrapModule function located in main.ts to start your app
*/
export class AppModule { }

服务

post(url: string, body: Object): Observable<any> {
    return this.http.post(this.apiUrl() + url, JSON.stringify(body), { headers: this.getHeaders() })
        .pipe(catchError(function (error: any) {
            return throwError(error || 'Server error');
        }));
}

app.gradle

android {
  defaultConfig {
    minSdkVersion 17
    generatedDensities = []
    flavorDimensions "versionCode"
  }
  aaptOptions {
    additionalParameters "--no-version-vectors"
  }
}

感谢您的宝贵时间

【问题讨论】:

  • API url 是什么样的?
  • 错误与 SSL 协议有关。也许您正在使用旧操作系统支持的 ssl 证书?
  • 我在 Api url 中使用 https,example.com.pe/back-app/api

标签: java android angular web-services nativescript


【解决方案1】:

试试这个:- 在应用模块中添加 HttpClientModule

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule, NativeScriptHttpClientModule, 
NativeScriptFormsModule } from "@nativescript/angular";
import { NgShadowModule } from "nativescript-ng-shadow";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { LoginComponent } from './components/login/login.component';
import { HeaderComponent } from 
  './components/partials/header/header.component';
import { FooterComponent } from 
  './components/partials/footer/footer.component';
import { IndexComponent } from './components/index/index.component';
import { RegistrarComponent } from 
  './components/registrar/registrar.component';
import { HttpClientModule } from "@angular/common/http";

 @NgModule({
 bootstrap: [
    AppComponent
 ],
 imports: [
    NativeScriptModule,
    AppRoutingModule,
    NativeScriptHttpClientModule,
    NgShadowModule,
    HttpClientModule,
    NativeScriptFormsModule
 ],
 declarations: [
    AppComponent,
    LoginComponent,
    HeaderComponent,
    FooterComponent,
    IndexComponent,
    RegistrarComponent,
 ],
 providers: [],
 schemas: [
     NO_ERRORS_SCHEMA
 ]
})
/*
Pass your application module to the bootstrapModule function located in 
main.ts to start your app
*/
export class AppModule { }

并在AndroidManifest.xml文件中添加一行:-

<application
    android:usesCleartextTraffic="true">

文件位置:- App_Resource->Android->src->main

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多