如何从url到离子变量获取变量?

时间:2017-09-26 14:24:46

标签: json angular ionic-framework ionic3

我正在使用admob插件处理Ionic 3项目。我创建两个变量(Mybanner和Myinterstital)存储admob代码,我想从外部URL获取这些变量的内容

http://example.com/admob.php

然后把它放在这里:

id: this.Myabanner,
id: this.Myinterstitial,

因为我想随时从网站更改admob代码。

import { AdMobFree, AdMobFreeBannerConfig, AdMobFreeInterstitialConfig, AdMobFreeRewardVideoConfig } from '@ionic-native/admob-free';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  Myabanner= "ca-app-pub-3940256099942544/6300978111";
  Myinterstitial= "ca-app-pub-3940256099942544/1033173712";
  // this tells the tabs component which Pages
  // should be each tab's root Page
  constructor(public navCtrl: NavController, private adMobFree: AdMobFree) {
    this.showInterstitialAd();
    this.ionViewDidLoad();
    this.showVideoRewardsAd()
  }
  ionViewDidLoad() {
    const bannerConfig: AdMobFreeBannerConfig = {
      id: this.Myabanner,
      isTesting: false,
      autoShow: true
    };
    this.adMobFree.banner.config(bannerConfig);
    this.adMobFree.banner.prepare()
      .then(() => {

      })
      .catch(e => console.log(e));

  }
  async showInterstitialAd() {
    try {
      const interstitialConfig: AdMobFreeInterstitialConfig = {
        id: this.Myinterstitial,
        isTesting: false,
        autoShow: true
      }

      this.adMobFree.interstitial.config(interstitialConfig);

      const result = await this.adMobFree.interstitial.prepare();
      console.log(result);
    }
    catch (e) {
      console.error(e)
    }
  }
  async showVideoRewardsAd() {
    try {
      const videoRewardsConfig: AdMobFreeRewardVideoConfig = {
        //id: 'ca-app-pub-3940256099942544/5224354917',
        isTesting: true,
        autoShow: true
      }

      this.adMobFree.rewardVideo.config(videoRewardsConfig);

      const result = await this.adMobFree.rewardVideo.prepare();
      console.log(result);
    }
    catch (e) {
      console.error(e);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

在PHP中,将AdMob代码分配到数组中,并将该数组转换为JSON输出。

在你的Ionic中,通过使用Http Cordova插件,你可以获得json值。

https://ionicframework.com/docs/native/http/

import { HTTP } from '@ionic-native/http';

constructor(private http: HTTP) { }

...

this.http.get('http://example.com/admob.php', {}, {})
.then(data => {

  console.log(data.status);
  console.log(data.data); // data received by server
  console.log(data.headers);

})

.catch(error => {

  console.log(error.status);
  console.log(error.error); // error message as string
  console.log(error.headers);

});