Http - 比特日记
Http
get
复制成功
1
2
3
4
5
6
1Button('发送网络请求')
2 .onClick(async (event: ClickEvent) => {
3 findDataByGetApi<Array<SwiperDataModel>>(1).then(res => {
4 this.swiperDataList = res
5 })
6 })
Button('发送网络请求')
.onClick(async (event: ClickEvent) => {
findDataByGetApi<Array<SwiperDataModel>>(1).then(res => {
this.swiperDataList = res
})
})
复制成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1Button('发送网络请求')
2 .onClick(async (event: ClickEvent) => {
3 const httpRequest = http.createHttp()
4 httpRequest.request('http://www.yyxx.org/data.php', {
5 method: http.RequestMethod.GET,
6 extraData: 'id=1'
7 }, (error: Error, data: http.HttpResponse) => {
8 if (!error) {
9 // {message:[{}]}, key:value
10 console.log(data.result.toString())
11 } else {
12 console.log('http error')
13 }
14 httpRequest.destroy()
15 })
16 //this.swiperData = res.message
17 })
Button('发送网络请求')
.onClick(async (event: ClickEvent) => {
const httpRequest = http.createHttp()
httpRequest.request('http://www.yyxx.org/data.php', {
method: http.RequestMethod.GET,
extraData: 'id=1'
}, (error: Error, data: http.HttpResponse) => {
if (!error) {
// {message:[{}]}, key:value
console.log(data.result.toString())
} else {
console.log('http error')
}
httpRequest.destroy()
})
//this.swiperData = res.message
})
复制成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
1Button('发送网络请求')
2 .onClick(async (event: ClickEvent) => {
3 const httpRequest = http.createHttp()
4 const res = new Promise((
5 resolve: (value: Record<string, string>) => void,
6 reject: (value ?: string) => void) => {
7 httpRequest.request('http://www.yyxx.org/data.php?id=1', {
8 method: http.RequestMethod.GET,
9 header: [{
10 "Content-type": "application/json"
11 }],
12 extraData: '',
13 connectTimeout: 6000,
14 readTimeout: 6000
15 }, (error: Error, data: http.HttpResponse) => {
16 if (!error) {
17 // {message:[{}]}, key:value
18 console.log(data.result.toString())
19 } else {
20 reject(error.message)
21 }
22 })
23 }
24 )
25 //this.swiperData = res.message
26 })
Button('发送网络请求')
.onClick(async (event: ClickEvent) => {
const httpRequest = http.createHttp()
const res = new Promise((
resolve: (value: Record<string, string>) => void,
reject: (value ?: string) => void) => {
httpRequest.request('http://www.yyxx.org/data.php?id=1', {
method: http.RequestMethod.GET,
header: [{
"Content-type": "application/json"
}],
extraData: '',
connectTimeout: 6000,
readTimeout: 6000
}, (error: Error, data: http.HttpResponse) => {
if (!error) {
// {message:[{}]}, key:value
console.log(data.result.toString())
} else {
reject(error.message)
}
})
}
)
//this.swiperData = res.message
})
HttpUtils
复制成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1import { http } from "@kit.NetworkKit"
2
3export function myRequest<T, K>(
4 url: string,
5 method: http.RequestMethod,
6 requestData ?: K
7) {
8 const httpRequest = http.createHttp()
9 return new Promise((
10 resolve: (value: Record<string, T>) => void,
11 reject: (value ?: string) => void) => {
12 httpRequest.request('http://www.yyxx.org/' + url, {
13 method: method,
14 header: [{
15 "Content-type": "application/json"
16 }],
17 extraData: JSON.stringify(requestData) || '',
18 connectTimeout: 6000,
19 readTimeout: 6000
20 }, (error: Error, data: http.HttpResponse) => {
21 if (!error) {
22 // {message:[{}]}, key:value
23 resolve(JSON.parse(data.result as string))
24 console.log(JSON.parse(data.result as string))
25 } else {
26 reject(error.message)
27 }
28 })
29 }
30 )
31}
32
33export function myRequestGet<T, K>(url: string, requestData?: K): Promise<Record<string, T>> {
34 return myRequest<T, K>(url, http.RequestMethod.GET, requestData)
35}
36
37export function myRequestPost<T, K>(url: string, requestData?: K): Promise<Record<string, T>> {
38 return myRequest<T, K>(url, http.RequestMethod.POST, requestData)
39}
40
41export function myRequestPut<T, K>(url: string, requestData?: K): Promise<Record<string, T>> {
42 return myRequest<T, K>(url, http.RequestMethod.PUT, requestData)
43}
44
45export function myRequestDelete<T, K>(url: string, requestData?: K): Promise<Record<string, T>> {
46 return myRequest<T, K>(url, http.RequestMethod.DELETE, requestData)
47}
import { http } from "@kit.NetworkKit"
export function myRequest<T, K>(
url: string,
method: http.RequestMethod,
requestData ?: K
) {
const httpRequest = http.createHttp()
return new Promise((
resolve: (value: Record<string, T>) => void,
reject: (value ?: string) => void) => {
httpRequest.request('http://www.yyxx.org/' + url, {
method: method,
header: [{
"Content-type": "application/json"
}],
extraData: JSON.stringify(requestData) || '',
connectTimeout: 6000,
readTimeout: 6000
}, (error: Error, data: http.HttpResponse) => {
if (!error) {
// {message:[{}]}, key:value
resolve(JSON.parse(data.result as string))
console.log(JSON.parse(data.result as string))
} else {
reject(error.message)
}
})
}
)
}
export function myRequestGet<T, K>(url: string, requestData?: K): Promise<Record<string, T>> {
return myRequest<T, K>(url, http.RequestMethod.GET, requestData)
}
export function myRequestPost<T, K>(url: string, requestData?: K): Promise<Record<string, T>> {
return myRequest<T, K>(url, http.RequestMethod.POST, requestData)
}
export function myRequestPut<T, K>(url: string, requestData?: K): Promise<Record<string, T>> {
return myRequest<T, K>(url, http.RequestMethod.PUT, requestData)
}
export function myRequestDelete<T, K>(url: string, requestData?: K): Promise<Record<string, T>> {
return myRequest<T, K>(url, http.RequestMethod.DELETE, requestData)
}
promise
复制成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1export function findDataByGetApi<T>(id: number): Promise<Record<string, T>> {
2 const baseUrl = 'http://www.yyxx.org/'
3 const url = 'data.php?id=' + id
4 const instance = axios.create({
5 baseURL: baseUrl,
6 timeout: 2000,
7 headers: {
8 "Content-type": "application/json"
9 }
10 })
11 return new Promise((
12 resolve: (value: Record<string, T>) => void,
13 reject: (value ?: string) => void) => {
14 instance.get(url).then((res: AxiosResponse) => {
15 resolve(JSON.parse(res.data as string))
16 }).catch((error: AxiosError) => {
17 console.log(`http error[${error}]`)
18 reject(error.message)
19 });
20 })
21}
export function findDataByGetApi<T>(id: number): Promise<Record<string, T>> {
const baseUrl = 'http://www.yyxx.org/'
const url = 'data.php?id=' + id
const instance = axios.create({
baseURL: baseUrl,
timeout: 2000,
headers: {
"Content-type": "application/json"
}
})
return new Promise((
resolve: (value: Record<string, T>) => void,
reject: (value ?: string) => void) => {
instance.get(url).then((res: AxiosResponse) => {
resolve(JSON.parse(res.data as string))
}).catch((error: AxiosError) => {
console.log(`http error[${error}]`)
reject(error.message)
});
})
}
Copyright ©2010-2022 比特日记 All Rights Reserved.
Powered By 可尔物语
浙ICP备11005866号-12