Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
httpClient.get在emulator上正常,但在android设备上不工作_Android_Angular_Ionic Framework - Fatal编程技术网

httpClient.get在emulator上正常,但在android设备上不工作

httpClient.get在emulator上正常,但在android设备上不工作,android,angular,ionic-framework,Android,Angular,Ionic Framework,我在emulator(ionic serve)上测试了我的应用程序,它运行良好。我的httpClient.get很好地返回了mySql数据库中的json数据。 但是,当我发布apk并将其安装到我的android设备上时,不会显示任何数据,我也不会成功捕获任何错误。 你能告诉我怎么解决这个问题吗 我的ts文件: import { Component} from '@angular/core'; import { Observable } from 'rxjs'; import Place from

我在emulator(ionic serve)上测试了我的应用程序,它运行良好。我的httpClient.get很好地返回了mySql数据库中的json数据。 但是,当我发布apk并将其安装到我的android设备上时,不会显示任何数据,我也不会成功捕获任何错误。 你能告诉我怎么解决这个问题吗

我的ts文件:

import { Component} from '@angular/core';
import { Observable } from 'rxjs';
import Place from '../../types';
import { throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
@Component({
  selector: 'app-debug',
  templateUrl: './debug.page.html',
  styleUrls: ['./debug.page.scss'],
})
export class DebugPage {
  places: Observable<Place[]>;
  APIPlacesDebug: string;
  error1: string='err1';
  error2: string='err2';
  error0: string='err0';
  constructor(
    private _httpClient: HttpClient
  ) {
    this.APIPlacesDebug  = "http://www.**********.be/v/**/call.php?query=placesDebug";
    this.places = this.getAllPlaces_fullDebug(); 
   }
   getAllPlaces_fullDebug(): Observable<Place[]>{
    return this._httpClient.get<Place[]>(this.APIPlacesDebug+'&full=Y')
    .pipe(catchError(this.handleError));
  }
   private handleError(error: HttpErrorResponse) {
    if (error.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
      console.error('An error occurred:', error.error.message);
      this.error0='1st:';
      this.error1= error.error.message;
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong.
      console.error(
        `Backend returned code ${error.status}, ` +
        `body was: ${error.error}`);
        this.error1=error.status+' ';
        this.error2=error.error;
        this.error0='2nd:';
    }
    // Return an observable with a user-facing error message.
    return throwError(
      'Something bad happened; please try again later.');
  }
}
从'@angular/core'导入{Component};
从“rxjs”导入{Observable};
从“../../types”导入位置;
从“rxjs”导入{throwError};
从“rxjs/operators”导入{catchError,重试};
从“@angular/common/http”导入{HttpClient,HttpErrorResponse};
@组成部分({
选择器:“应用程序调试”,
templateUrl:“./debug.page.html”,
样式URL:['./debug.page.scss'],
})
导出类调试页面{
地点:可见;
apisplacesdebug:字符串;
error1:string='err1';
error2:string='err2';
error0:string='err0';
建造师(
私有\u httpClient:httpClient
) {
this.apisplacesdebug=”http://www.**********.be/v/**/call.php?query=placesDebug”;
this.places=this.getAllPlaces_fullDebug();
}
getAllPlaces_fullDebug():可观察{
返回此。\u httpClient.get(this.apisplacesdebug+'&full=Y')
.pipe(catchError(this.handleError));
}
私有句柄错误(错误:HttpErrorResponse){
if(error.error instanceof ErrorEvent){
//发生客户端或网络错误。请相应地进行处理。
console.error('发生错误:',error.error.message);
这个.error0='1st:';
this.error1=error.error.message;
}否则{
//后端返回了一个不成功的响应代码。
//回应主体可能包含错误原因的线索。
控制台错误(
`后端返回代码${error.status},`+
`正文是:${error.error}`);
this.error1=error.status+“”;
this.error2=error.error;
这个.error0='2nd:';
}
//返回带有面向用户的错误消息的可观察对象。
回击投手(
“发生了不好的情况;请稍后再试。”);
}
}
我的html文件:

<ion-header [translucent]="true">
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    <ion-title>Debug</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content padding class="form-content" [fullscreen]="true">
    <ion-header collapse="condense">
      <ion-toolbar>
        <ion-title size="large">Debug</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-item>
      <ion-label color="primary">{{error0}}</ion-label>
    </ion-item>    
    <ion-item>
      <ion-label color="primary">{{error1}}</ion-label>
    </ion-item>
    <ion-item>
      <ion-label color="primary">{{error2}}</ion-label>
    </ion-item>
      <ion-item *ngFor="let place of (places | async)">
        <ion-label color="primary">{{place.name}}</ion-label>
      </ion-item>
</ion-content>

调试
调试
{{error0}}
{{error1}}
{{error2}}
{{place.name}
请帮忙:)
Vincent

默认情况下,Android(https)上只支持安全URL,而您似乎正在使用不安全URL(http)

您可以尝试:

  • 将URL更改为https(如果可能)

  • 更改config.xml以允许不安全的URL(
    android:usesCleartextTraffic
    ),如下所述: