鸿蒙Harmony OS Next原生开发天气预报项目完整封装并获取数据对象

鸿蒙初开,开天辟地

 

import { weatherModel } from './weatherModel'
import http from '@ohos.net.http';
import key from '../const/key';

class getWeatherUtil{
  // 发送一个URL请求,返回对应的数据
  getWeather(cityCode:number){
    return new Promise((resolve,reject)=>{
      let request = http.createHttp();
      let url = `https://restapi.amap.com/v3/weather/weatherInfo?city=${cityCode}&extensions=all&key=${key}`;
      let result = request.request(url);
      result.then((res)=>{
        if(res.responseCode===200){
          resolve(JSON.parse(res.result.toString()));
        }
      }).catch((err:object)=>{
        console.log(err.toString());
      });
    });
  }
  // 多个URL,一并返回结果
  async getWeathers(cityCodes:Array){
    let promises:Array<Promise> = [];
    let weatherModels:Array = [];
    for (let i = 0; i < citycodes.length i promises.pushthis.getweathercitycodesi await> {
      weatherModels = result;
    });
    return weatherModels;
  }
}

export default new getWeatherUtil() as getWeatherUtil;

import { weatherModel } from './weatherModel'
import http from '@ohos.net.http';
import key from '../const/key';

class getWeatherUtil{
  // 发送一个URL请求,返回对应的数据
  getWeather(cityCode:number){
    return new Promise((resolve,reject)=>{
      let request = http.createHttp();
      let url = `https://restapi.amap.com/v3/weather/weatherInfo?city=${cityCode}&extensions=all&key=${key}`;
      let result = request.request(url);
      result.then((res)=>{
        if(res.responseCode===200){
          resolve(JSON.parse(res.result.toString()));
        }
      }).catch((err:object)=>{
        console.log(err.toString());
      });
    });
  }
  // 多个URL,一并返回结果
  async getWeathers(cityCodes:Array){
    let promises:Array<Promise> = [];
    let weatherModels:Array = [];
    for (let i = 0; i < citycodes.length i promises.pushthis.getweathercitycodesi await> {
      weatherModels = result;
    });
    return weatherModels;
  }
}

export default new getWeatherUtil() as getWeatherUtil;

import { cast } from './cast';

export class forecast {
  city: string;
  adcode: number;
  casts: Array;
  constructor(city: string, adcode: number, casts: Array) {
    this.city = city;
    this.adcode = adcode;
    this.casts = casts;
  }
}

import { cast } from './cast';

export class forecast {
  city: string;
  adcode: number;
  casts: Array;
  constructor(city: string, adcode: number, casts: Array) {
    this.city = city;
    this.adcode = adcode;
    this.casts = casts;
  }
}

 

export class cast {
  date: string;
  week: number;
  dayweather: string;
  nightweather: string;
  daytemp: number;
  nighttemp: number;
  daywind: string;
  nightwind: string;
  daypower: string;
  nightpower: string;
  daytemp_float: number;
  nighttemp_float: number;

  constructor(date: string, week: number, dayweather: string, nightweather: string, daytemp: number, nighttemp: number, daywind: string, nightwind: string, daypower: string, nightpower: string, daytemp_float: number, nighttemp_float: number) {
    this.date = date;
    this.week = week;
    this.dayweather = dayweather;
    this.nightweather = nightweather;
    this.daytemp = daytemp;
    this.nighttemp = nighttemp;
    this.daywind = daywind;
    this.nightwind = nightwind;
    this.daypower = daypower;
    this.nightpower = nightpower;
    this.daytemp_float = daytemp_float;
    this.nighttemp_float = nighttemp_float;
  }
}

export class cast {
  date: string;
  week: number;
  dayweather: string;
  nightweather: string;
  daytemp: number;
  nighttemp: number;
  daywind: string;
  nightwind: string;
  daypower: string;
  nightpower: string;
  daytemp_float: number;
  nighttemp_float: number;

  constructor(date: string, week: number, dayweather: string, nightweather: string, daytemp: number, nighttemp: number, daywind: string, nightwind: string, daypower: string, nightpower: string, daytemp_float: number, nighttemp_float: number) {
    this.date = date;
    this.week = week;
    this.dayweather = dayweather;
    this.nightweather = nightweather;
    this.daytemp = daytemp;
    this.nighttemp = nighttemp;
    this.daywind = daywind;
    this.nightwind = nightwind;
    this.daypower = daypower;
    this.nightpower = nightpower;
    this.daytemp_float = daytemp_float;
    this.nighttemp_float = nighttemp_float;
  }
}

import { forecast } from './forecast';

export class weatherModel {
  status: string;
  count: number;
  infocode: number;
  forecasts: Array = [];

  constructor(status: string, count: number, infocode: number, forecasts: Array) {
    this.status = status;
    this.count = count;
    this.infocode = infocode;
    this.forecasts = forecasts;
  }
}

import { forecast } from './forecast';

export class weatherModel {
  status: string;
  count: number;
  infocode: number;
  forecasts: Array = [];

  constructor(status: string, count: number, infocode: number, forecasts: Array) {
    this.status = status;
    this.count = count;
    this.infocode = infocode;
    this.forecasts = forecasts;
  }
}

import getWeatherUtil from "../viewmodel/getWeatherUtil";
@Entry
@Component
struct Index {
  build() {
    Column(){
      Button("测试按钮").fontSize(40)
        .onClick(()=>{
          getWeatherUtil.getWeather(120000);
        })
      Button("获取城市名").fontSize(40)
        .onClick(()=>{
          getWeatherUtil.getWeathers([120000,110000]);
        })
    }
  }
}

import getWeatherUtil from "../viewmodel/getWeatherUtil";
@Entry
@Component
struct Index {
  build() {
    Column(){
      Button("测试按钮").fontSize(40)
        .onClick(()=>{
          getWeatherUtil.getWeather(120000);
        })
      Button("获取城市名").fontSize(40)
        .onClick(()=>{
          getWeatherUtil.getWeathers([120000,110000]);
        })
    }
  }
}

完整封装并获取数据对象

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值