Ionic应用程式可在网路上运作,但无法在模拟器或装置上运作

时间:2019-07-13 15:03:29

标签: android android-studio ionic-framework device simulator

我正在用Ionic开发一个简单的女仆移动应用程序。我已经开发了注册模块,并且可以在Web上正常工作,但是当我尝试在设备或模拟器中构建它时,它将无法正常工作。

I am getting this error when I run this command(ionic cordova run android --livereload

I am getting this error when I run this command(ionic cordova run android --livereload

当我在模拟器中运行(ionic cordova android)时,应用程序加载但无法正常运行,这意味着当我输入用户信息并单击“提交”时,该信息未加载到数据库中,而当我在其中执行相同操作时网络(离子服务)工作得很好,我可以将新用户注册到数据库中。我在后端使用laravel。请帮我解决这个问题。

index.html

<!DOCTYPE html>
<html lang="en">

<head>
  
  <meta charset="utf-8" />
  <title>Ionic App</title>

  <base href="."/>

  <meta name="viewport" content="viewport-fit=cover, width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="msapplication-tap-highlight" content="no" />


  <link rel="icon" type="image/png" href="assets/icon/favicon.png" />
  

  <!-- add to homescreen for ios -->
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
</head>

<body>
  <app-root></app-root>
</body>

</html>

config.xml

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.ionic.starter" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>MyApp</name>
    <description>An awesome Ionic/Cordova app.</description>
    <author email="hi@ionicframework.com" href="http://ionicframework.com/">Ionic Framework Team</author>
    <content src="index.html" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <preference name="ScrollEnabled" value="false" />
    <preference name="android-minSdkVersion" value="19" />
    <preference name="BackupWebStorage" value="none" />
    <preference name="SplashMaintainAspectRatio" value="true" />
    <preference name="FadeSplashScreenDuration" value="300" />
    <preference name="SplashShowOnlyFirstTime" value="false" />
    <preference name="SplashScreen" value="screen" />
    <preference name="SplashScreenDelay" value="3000" />
    <platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application" xmlns:android="http://schemas.android.com/apk/res/android">
            <application android:networkSecurityConfig="@xml/network_security_config" />
            <application android:usesCleartextTraffic="true" />
        </edit-config>
        <resource-file src="resources/android/xml/network_security_config.xml" target="app/src/main/res/xml/network_security_config.xml" />
        <allow-intent href="market:*" />
        <icon density="ldpi" src="resources\android\icon\drawable-ldpi-icon.png" />
        <icon density="mdpi" src="resources\android\icon\drawable-mdpi-icon.png" />
        <icon density="hdpi" src="resources\android\icon\drawable-hdpi-icon.png" />
        <icon density="xhdpi" src="resources\android\icon\drawable-xhdpi-icon.png" />
        <icon density="xxhdpi" src="resources\android\icon\drawable-xxhdpi-icon.png" />
        <icon density="xxxhdpi" src="resources\android\icon\drawable-xxxhdpi-icon.png" />
        <splash density="land-ldpi" src="resources\android\splash\drawable-land-ldpi-screen.png" />
        <splash density="land-mdpi" src="resources\android\splash\drawable-land-mdpi-screen.png" />
        <splash density="land-hdpi" src="resources\android\splash\drawable-land-hdpi-screen.png" />
        <splash density="land-xhdpi" src="resources\android\splash\drawable-land-xhdpi-screen.png" />
        <splash density="land-xxhdpi" src="resources\android\splash\drawable-land-xxhdpi-screen.png" />
        <splash density="land-xxxhdpi" src="resources\android\splash\drawable-land-xxxhdpi-screen.png" />
        <splash density="port-ldpi" src="resources\android\splash\drawable-port-ldpi-screen.png" />
        <splash density="port-mdpi" src="resources\android\splash\drawable-port-mdpi-screen.png" />
        <splash density="port-hdpi" src="resources\android\splash\drawable-port-hdpi-screen.png" />
        <splash density="port-xhdpi" src="resources\android\splash\drawable-port-xhdpi-screen.png" />
        <splash density="port-xxhdpi" src="resources\android\splash\drawable-port-xxhdpi-screen.png" />
        <splash density="port-xxxhdpi" src="resources\android\splash\drawable-port-xxxhdpi-screen.png" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
        <icon height="57" src="resources/ios/icon/icon.png" width="57" />
        <icon height="114" src="resources/ios/icon/icon@2x.png" width="114" />
        <icon height="40" src="resources/ios/icon/icon-40.png" width="40" />
        <icon height="80" src="resources/ios/icon/icon-40@2x.png" width="80" />
        <icon height="120" src="resources/ios/icon/icon-40@3x.png" width="120" />
        <icon height="50" src="resources/ios/icon/icon-50.png" width="50" />
        <icon height="100" src="resources/ios/icon/icon-50@2x.png" width="100" />
        <icon height="60" src="resources/ios/icon/icon-60.png" width="60" />
        <icon height="120" src="resources/ios/icon/icon-60@2x.png" width="120" />
        <icon height="180" src="resources/ios/icon/icon-60@3x.png" width="180" />
        <icon height="72" src="resources/ios/icon/icon-72.png" width="72" />
        <icon height="144" src="resources/ios/icon/icon-72@2x.png" width="144" />
        <icon height="76" src="resources/ios/icon/icon-76.png" width="76" />
        <icon height="152" src="resources/ios/icon/icon-76@2x.png" width="152" />
        <icon height="167" src="resources/ios/icon/icon-83.5@2x.png" width="167" />
        <icon height="29" src="resources/ios/icon/icon-small.png" width="29" />
        <icon height="58" src="resources/ios/icon/icon-small@2x.png" width="58" />
        <icon height="87" src="resources/ios/icon/icon-small@3x.png" width="87" />
        <icon height="1024" src="resources/ios/icon/icon-1024.png" width="1024" />
        <splash height="1136" src="resources/ios/splash/Default-568h@2x~iphone.png" width="640" />
        <splash height="1334" src="resources/ios/splash/Default-667h.png" width="750" />
        <splash height="2208" src="resources/ios/splash/Default-736h.png" width="1242" />
        <splash height="1242" src="resources/ios/splash/Default-Landscape-736h.png" width="2208" />
        <splash height="1536" src="resources/ios/splash/Default-Landscape@2x~ipad.png" width="2048" />
        <splash height="2048" src="resources/ios/splash/Default-Landscape@~ipadpro.png" width="2732" />
        <splash height="768" src="resources/ios/splash/Default-Landscape~ipad.png" width="1024" />
        <splash height="2048" src="resources/ios/splash/Default-Portrait@2x~ipad.png" width="1536" />
        <splash height="2732" src="resources/ios/splash/Default-Portrait@~ipadpro.png" width="2048" />
        <splash height="1024" src="resources/ios/splash/Default-Portrait~ipad.png" width="768" />
        <splash height="960" src="resources/ios/splash/Default@2x~iphone.png" width="640" />
        <splash height="480" src="resources/ios/splash/Default~iphone.png" width="320" />
        <splash height="2732" src="resources/ios/splash/Default@2x~universal~anyany.png" width="2732" />
    </platform>
    <plugin name="cordova-plugin-whitelist" spec="1.3.3" />
    <plugin name="cordova-plugin-statusbar" spec="2.4.2" />
    <plugin name="cordova-plugin-device" spec="2.0.2" />
    <plugin name="cordova-plugin-splashscreen" spec="5.0.2" />
    <plugin name="cordova-plugin-ionic-webview" spec="^4.0.0" />
    <plugin name="cordova-plugin-ionic-keyboard" spec="^2.0.5" />
    <plugin name="cordova-plugin-advanced-http" spec="^2.1.1">
        <variable name="OKHTTP_VERSION" value="3.10.0" />
    </plugin>
    <engine name="browser" spec="^5.0.4" />
    <engine name="android" spec="^7.1.4" />
</widget>

auth-service.ts

import { Injectable, OnInit } from '@angular/core';
import 'rxjs/add/operator/map';
import { HTTP } from '@ionic-native/http/ngx';
import {Headers, RequestOptions } from '@angular/http';
import { HttpClient } from '@angular/common/http';

const apiUrl = 'http://fypBackend.test/api/users';

@Injectable({
  providedIn: 'root'
})
export class AuthServiceService implements OnInit {
  regData = {"name": "", "icNumber": "", "email": "", "password": "", "phone": "", "address": "", "cityState": "", "houseType": "", "category":""};
  data = {
    "name": this.regData.name,
    "icNumber": this.regData.icNumber,
    "email": this.regData.email,
    "password": this.regData.password,
    "phone": this.regData.phone,
    "address": this.regData.address,
    "cityState": this.regData.cityState,
    "houseType": this.regData.houseType,
    "category": this.regData.category
  };

  constructor(public http: HttpClient) { }
  ngOnInit(){}

  register(data){
    return new Promise(() => {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json' );
    
    this.http.post(apiUrl, data)
    .subscribe(data => {
      console.log(data);
      console.log('Data inserted');
    }, error => {
     console.log(data.error);
     console.log(error);
     console.log(error.status);
     console.log(error.error); // error message as string
     console.log(error.headers);
     console.log('Not working')
   });
  })
}
}

register.ts

import { Component} from '@angular/core';
import { NavController,LoadingController, ToastController } from '@ionic/angular';
import {AuthServiceService} from '../auth-service.service';
import { AlertController } from '@ionic/angular';


@Component({
  selector: 'page-register',
  templateUrl: './register.page.html',
  styleUrls: ['./register.page.scss'],
})
export class RegisterPage{
  loading: any;
  regData = {"name": "", "icNumber": "", "email": "", "password": "", "phone": "", "address": "", "cityState": "", "houseType": "", "category":""};

  constructor(public navCtrl: NavController, public authService: AuthServiceService, public loadingCtr: LoadingController, private toastCtrl: ToastController, public alertController:AlertController) { }

  doSignup(){
    this.authService.register(this.regData).then((result)=>{
      this.presentAlertSuccess();
      console.log('Successful')
    }, (err) => {
      this.presentAlert(err);
      console.log("There is an error")
    });
  }
  async presentAlertSuccess(){
    const alert = await this.alertController.create({
      header: 'Alert',
      subHeader: 'Subtitle',
      message: 'Successful',
      buttons: ['OK']
    });

    await alert.present();
  }
  async presentAlert(msg){
    const alert = await this.alertController.create({
      header: 'Alert',
      subHeader: 'Subtitle',
      message: 'Not successful',
      buttons: ['OK']
    });

    await alert.present();
  }
}

离子信息

Ionic:

   Ionic CLI                     : 5.2.1 (C:\Users\User\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework               : @ionic/angular 4.5.0
   @angular-devkit/build-angular : 0.13.9
   @angular-devkit/schematics    : 7.3.9
   @angular/cli                  : 7.3.9
   @ionic/angular-toolkit        : 1.5.1

Cordova:

   Cordova CLI       : 8.1.2 (cordova-lib@8.1.1)
   Cordova Platforms : android 7.1.4, browser 5.0.4
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.1, (and 6 other plugins)

Utility:

   cordova-res : 0.3.0
   native-run  : 0.2.6

System:

   Android SDK Tools : 26.0.2 (C:\Users\User\AppData\Local\Android\Sdk)
   NodeJS            : v10.3.0 (C:\Program Files\nodejs\node.exe)
   npm               : 6.1.0
   OS                : Windows 10

3 个答案:

答案 0 :(得分:0)

首先,您应该检查chrome://inspect/并查看其内容, 但是如果您使用“ build”,也可以使用ionic cordova emulate androidionic cordova build android并在模拟器中安装应用程序 您的应用apk将位于

"yourporject_path/platforms/android/app/build/outputs/apk"

答案 1 :(得分:0)

npm缓存清理--force
npm install -g @ angular / cli

OR

npm缓存清理--force

打开cmd并按照说明进行操作

1-离子启动myApp选项卡--type = ionic-angular
2-将新应用程序与Capacitor集成在一起,以定位本机iOS和Android?没有
3-cd myApp(访问离子项目)
4-离子发球
5-离子Cordova平台添加android

答案 2 :(得分:0)

将以下行添加到根文件夹中的 config.xml。 这是在引发错误之前加载页面时等待的时间(以毫秒为单位):cordova.apache.org/docs/en/latest/config_ref/index.html

    <preference name="loadUrlTimeoutValue" value="120000" />

这可能是由于以下原因造成的

核心原因:问题很可能是由于模拟器速度太慢导致无法及时完成通信。

这可能是由于:

您的代码/数据/图像太大了。 您的脚本可能有一个无限循环或长循环,因此加载时间太长。 您将使用过多的脚本(jQuery、iscroll 等。更多的插件或脚本)