API calls made in Ionic 4 app not working on Andro

2020-03-26 10:36发布

问题:

I am able to run my ionic app on my laptop by using the ionic lab command.

The app makes calls to the IMDB api, & displays data retrieved from it.

Now, I am trying to run the app on an android device. by using the following command: ionic cordova run android --device.

The app appears on my phone, but when I search for IMDB data, no results are appearing.

Below is the service I am using to make calls to the API:

export class MovieService {

  url = 'http://www.omdbapi.com/';
  apiKey = 'myKey';

  constructor(private http: HttpClient) { }

  searchData(title: string, type: SearchType): Observable<any> {
    return this.http.get(`${this.url}?s=${encodeURI(title)}&type=${type}&apikey=${this.apiKey}`)
      .pipe(
        map(results => {
          console.log('RAW', results);
          return results['Search'];
        })
      );
  }

  getDetails(id): Observable<any> {
    return this.http.get(`${this.url}?i=${id}&plot=full&apikey=${this.apiKey}`);
  }
}

Can someone please tell me why the app is working on my laptop, but when I try to use it on my phone it isn't working as expected?

回答1:

Probably one reason is that you are using http not https.

Android restricted access to non-secure feeds in a recent security update.

Try:

url = 'https://www.omdbapi.com/';