已开启
api突然获取不了数据 #12
AtomGit-Bot创建于  2023年7月21日
AtomGit-Bot
2023年7月21日 创建

【任务描述】
原来是获取到数据了,后来突然获取不到数据了。
import http from '@ohos.net.http';
const key = 'ae01ea4d90a449caa55e91147b3f3e5a'

// 实时天气
export function getNowWeather(location: number): Promise<http.HttpResponse> {
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest = http.createHttp();
return httpRequest.request(
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"https://devapi.qweather.com/v7/weather/now?location=" + location + "&key=" + key,
{
method: http.RequestMethod.GET, // 可选,默认为http.RequestMethod.GET
expectDataType: http.HttpDataType.OBJECT, // 可选,指定返回数据的类型
});
}

// 7日天气
export function get7dWeather(location: number): Promise<http.HttpResponse> {
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest = http.createHttp();
return httpRequest.request(
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"https://devapi.qweather.com/v7/weather/7d?location=" + location + "&key=" + key,
{
method: http.RequestMethod.GET, // 可选,默认为http.RequestMethod.GET
expectDataType: http.HttpDataType.OBJECT, // 可选,指定返回数据的类型
});
}

// 24小时天气预报
export function get24hWeather(location: number): Promise<http.HttpResponse> {
// 每一个httpRequest对应一个HTTP请求任务,不可复用
let httpRequest = http.createHttp();
return httpRequest.request(
// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
"https://devapi.qweather.com/v7/weather/24h?location=" + location + "&key=" + key,
{
method: http.RequestMethod.GET, // 可选,默认为http.RequestMethod.GET
expectDataType: http.HttpDataType.OBJECT, // 可选,指定返回数据的类型
});
}
和axios
import axios from '../Axios'
import { AxiosResponse} from '@ohos/axios'
// 实时天气
export function getNowWeather(location: number): Promise {
let promise = axios({
url: "/now",
params:{
location: location,
}
})
promise.then((data=>{
console.info('successful return'+JSON.stringify(data))
})).catch((err)=>{
console.error('failed to return err '+JSON.stringify(err))
})
return promise
}
// 7日天气
export function get7dWeather(location: number): Promise {
return axios({
url: "/7d",
params:{
location: location,
}
})
}

// 24小时天气预报
export function get24hWeather(location: number): Promise {
return axios({
url: "/24h",
params:{
location: location,
}
})
}

import axios from '@ohos/axios'
axios.defaults.baseURL = 'https://devapi.qweather.com/v7/weather'
const key = 'a546dd6657824b6882ec461255642293'
// 添加请求拦截器
axios.interceptors.request.use(config => {
// 对请求数据做点什么
config.params.key = key
return config;
}, error => {
// 对请求错误做些什么
return Promise.reject(error);
});
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
// 对响应数据做点什么
return response;
}, function (error) {
// 对响应错误做点什么
console.error('axios response error:' + error)
return Promise.reject(error);
});
export default axios

【解决方案】

【任务来源】

likedislike
AAtomGit-Bot
2023年7月21日 创建了任务