IONIC 3:生成APK文件时的UnhandledPromiseRejectionWarning

时间:2018-03-09 20:46:40

标签: java android ionic-framework ionic2 ionic3

生成 apk 文件时遇到问题。我收到以下错误。

命令:ionic cordova build android

输出:

> cordova build android
Android Studio project detected

ANDROID_HOME=C:\Users\****\AppData\Local\Android\Sdk
JAVA_HOME=C:\Program Files\Java\jdk-9.0.4
(node:17504) UnhandledPromiseRejectionWarning: Unhandled promise rejection (reje
ction id: 1): CordovaError: Requirements check failed for JDK 1.8 or greater
(node:17504) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate th
e Node.js process with a non-zero exit code.

[13:47:49] lint在8.47秒完成

这是我的休息文件rest.ts

的内容
    import { HttpClient } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs/Observable';
    import 'rxjs/add/operator/catch';
    import 'rxjs/add/operator/map';
    import 'rxjs/add/operator/toPromise';

    @Injectable()
    export class RestProvider {

      private baseUrl = 'http://localhost/project/web/rest/mobile/v1/';
      private nomsvillesUrl = this.baseUrl + 'ville/nomsvilles/1';

      constructor(public http: HttpClient) {
        console.log('Hello RestProvider Provider');
      }

      getNomvilles(): Observable<string[]> {
        return this.http.get(this.nomsvillesUrl)
                        .map(this.extractData)
                        .catch(this.handleError);
      }

      private extractData(res: Response) {
        let body = res;
        return body || { };
      }

      private handleError (error: Response | any) {
        let errMsg: string;
        if (error instanceof Response) {
          const err = error || '';
          errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
        } else {
          errMsg = error.message ? error.message : error.toString();
        }
        console.error(errMsg);
        return Observable.throw(errMsg);
      }

}

这是我的主要课程main.ts

的内容
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { RestProvider } from '../../providers/rest/rest';

@Component({
  selector: 'page-main',
  templateUrl: 'main.html',
})
export class MainPage {

  villes: string[]
  errorMessage: string

  constructor(public navCtrl: NavController, public navParams: NavParams, public rest: RestProvider) {
  }

  ionViewDidLoad() {
    this.getVilles();
  }

  getVilles() {
    this.rest.getNomvilles().subscribe(
            villes => this.villes = villes,
            error => this.errorMessage = <any>error
        );
    }

}

请帮帮我!我想知道如何处理Promise以防止拒绝承诺。

谢谢。

1 个答案:

答案 0 :(得分:1)

错误与您的代码无关。它要求 jdk 1.8 或更高,您可以从此link下载它。但首先卸载你正在使用的jdk 1.9,因为它与android不兼容。

然后创建一个环境变量JAVA_HOME=C:\path\to\jdk\bin