一、常量类 Constants.ets
//超时时间10秒
static readonly HTTP_READ_TIMEOUT: number = 10000;
//请求成功返回码
static readonly HTTP_CODE_200: number = 200;
//请求成功后data中的code值(根据实际情况定)
static readonly SERVER_CODE_SUCCESS: number = 0;
二、请求工具类HttpUtil.ets
import { http } from '@kit.NetworkKit';
import ResponseResult from '../bean/ResponseResult';
import Constants, { ContentType } from '../constants/Constants';
//Get请求
export function httpRequestGet(url: string) {
return httpRequest(url, http.RequestMethod.GET);
}
//Post请求
export function httpRequestPost(url: string, requestData: Object) {
return httpRequest(url, http.RequestMethod.POST, requestData);
}
//处理请求参数和请求结果
function httpRequest(url: string, method: http.RequestMethod, params?: Object): Promise<ResponseResult> {
let httpRequest = http.createHttp();
let responseResult = httpRequest.request(url, {
method: method,
readTimeout: Constants.HTTP_READ_TIMEOUT,
header: {
'Content-Type': ContentType.JSON,
},
connectTimeout: Constants.HTTP_READ_TIMEOUT,
extraData: params
});
let serverData = new ResponseResult();
return responseResult.then((value: http.HttpResponse) => {
let result = `${value.result}`;
let resultJson: ResponseResult = JSON.parse(result);
if (value.responseCode === Constants.HTTP_CODE_200) {
if (resultJson.code === Constants.SERVER_CODE_SUCCESS) {
serverData.value = resultJson.value;
}
serverData.code = resultJson.code;
serverData.msg = resultJson.msg;
} else {
serverData.code = resultJson.code;
serverData.msg = resultJson.msg;
}
return serverData;
}).catch(() => {
return serverData;
});
}
三、使用示例
1、可新建一个ets文件存放请求对象RequestBean和请求接收ResponseBean等对象
//DataPage类
export class DataPage<T> {
pageIndex: number = 1; //页码
pageSize: number = 10; //页面大小
pageTotal: number = 1; //总页数
amount: number = 0; //记录总数
data: T[] = []; //本页数据
}
//请求Bean
export class ActivityRequest {
token: string = "";
clientType: string = "";
page: number = 1;
}
//请求接收Bean
export class ActivityResponse {
id: number = 0;
mainActivityId: number = 0;
name: string = "";
.......
}
2、编写请求方法,传入请求参数,设置Url,处理请求结果等
RequestData(page: number): Promise<DataPage<ActivityResponse>> {
return new Promise((resolve: Function) => {
let url = '请求Url.....';
let requestData: ActivityRequest = new ActivityRequest();
requestData.clientType = 'HarmonyOS';
PreferencesUtil.getLoginToken().then((value) => {
requestData.token = value;
requestData.page = page;
Logger.debug(this.TAG,'请求Url:'+ url)
Logger.debug(this.TAG,'请求参数信息:'+ JSON.stringify(requestData))
//这里调用HttpUtil工具类里面的Post请求方法
httpRequestPost(url, requestData).then((data: ResponseResult) => {
if (data && data.code === Constants.SERVER_CODE_SUCCESS) {
resolve(data.value);
Logger.debug(this.TAG,'请求成功!')
//可以在此处处理请求结果,也可以在步骤3所示请求回调中处理请求的结果
} else {
Logger.debug(this.TAG,'请求失败!' + data.code + ": " + data.msg)
resolve(null);
if (data.code === -1) {
showToast('请求异常')
}else {
showToast(data.msg)
}
}
}).catch(() => {
Logger.error(this.TAG,'请求异常!')
showToast('网络请求异常!')
resolve(null);
});
});
});
}
3、调用,返回请求成功后的数据结果,可进行后续处理
this.RequestData(page).then((result: DataPage<ActivityResponse>) => {
if (result){
Logger.debug(this.TAG,'页码pageIndex:'+ result.pageIndex + ' 每页数量pageSize:'+ result.pageSize + ' 总页数pageTotal:'+ result.pageTotal + '记录总数amount:' + result.amount)
if(result.amount > 0) {
Logger.debug(this.TAG,'数据:'+ JSON.stringify(this.liveDataList))
}else{
Logger.debug(this.TAG,'数据为空')
}
}
});