页面import { http } from ‘@kit.NetworkKit’;
import { JSON } from ‘@kit.ArkTS’;
@Entry
@Component
struct SearchCity {
@State changeValue: string = ‘’
@State submitValue: string = ‘’
myKey : String = ‘af803f0eb311454d9c407edea573568b’
controller: SearchController = new SearchController()
build() {
Column({space: 10}) {
Search({ value: this.changeValue, placeholder: ‘搜索城市’, controller: this.controller })
.searchButton(‘搜索’)
.width(‘95%’)
.height(40)
.backgroundColor(’#F5F5F5’)
.placeholderColor(Color.Grey)
.placeholderFont({ size: 14, weight: 400 })
.textFont({ size: 14, weight: 400 })
.onSubmit((value: string) => {
this.submitValue = value;
let cityName = encodeURIComponent(this.submitValue);
let myUrl: string = “https://geoapi.qweather.com/v2/city/lookup?location=” + cityName + “&key=” + this.myKey + “&number=1”;
this.getLocation(myUrl);
})
.onChange((value: string) => {
this.changeValue = value
})
.margin(20)
}.width(‘100%’)
}
getLocation(url:string){
let httpRequest = http.createHttp();
console.info(url);
let promise = httpRequest.request(url, {
method: http.RequestMethod.GET
});
promise.then((value) => {
if(value.responseCode === 200){
console.info(‘Result:’ + JSON.stringify(value.result));
}
})
httpRequest.destroy();
}
}
module.json5
{
“module”: {
“name”: “entry”,
“type”: “entry”,
“description”: “$string:module_desc”,
“mainElement”: “EntryAbility”,
“deviceTypes”: [
“phone”,
“tablet”,
“2in1”
],
“deliveryWithInstall”: true,
“installationFree”: false,
“pages”: “$profile:main_pages”,
“abilities”: [
{
“name”: “EntryAbility”,
“srcEntry”: “./ets/entryability/EntryAbility.ets”,
“description”: “$string:EntryAbility_desc”,
“icon”: “$media:layered_image”,
“label”: “$string:EntryAbility_label”,
“startWindowIcon”: “$media:startIcon”,
“startWindowBackground”: “$color:start_window_background”,
“exported”: true,
“skills”: [
{
“entities”: [
“entity.system.home”
],
“actions”: [
“action.system.home”
]
}
]
}
],
“extensionAbilities”: [
{
“name”: “EntryBackupAbility”,
“srcEntry”: “./ets/entrybackupability/EntryBackupAbility.ets”,
“type”: “backup”,
“exported”: false,
“metadata”: [
{
“name”: “ohos.extension.backup”,
“resource”: “$profile:backup_config”
}
],
}
],
“requestPermissions”: [
{“name” : “ohos.permission.INTERNET”,
“usedScene”: {
“when”: “always”
}
},
{
“name”: “ohos.permission.GET_NETWORK_INFO”
}
]
}
}