add convert
This commit is contained in:
parent
3a4338c99a
commit
9010489037
@ -3,6 +3,7 @@ import axios, { AxiosError, AxiosResponse } from '@ohos/axios'
|
|||||||
import http from '@ohos.net.http';
|
import http from '@ohos.net.http';
|
||||||
import { RestAPI } from './RestAPI';
|
import { RestAPI } from './RestAPI';
|
||||||
import { Organization } from './APIModel';
|
import { Organization } from './APIModel';
|
||||||
|
import { convertxml, util } from '@kit.ArkTS';
|
||||||
|
|
||||||
const TAG = '[RestAPIPage]'
|
const TAG = '[RestAPIPage]'
|
||||||
|
|
||||||
@ -39,7 +40,7 @@ struct RestAPIPage {
|
|||||||
})
|
})
|
||||||
|
|
||||||
Button("GetByHttp").onClick(() =>{
|
Button("GetByHttp").onClick(() =>{
|
||||||
this.viewModel.getAllOrgs();
|
this.viewModel.getServerVersionByHttp();
|
||||||
}).margin({top: 20})
|
}).margin({top: 20})
|
||||||
}.width('80%')
|
}.width('80%')
|
||||||
}.width('100%')
|
}.width('100%')
|
||||||
@ -64,5 +65,31 @@ class RestAPIViewModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getServerVersionByHttp() {
|
getServerVersionByHttp() {
|
||||||
|
let request = http.createHttp();
|
||||||
|
let header: Map<string, string> = new Map();
|
||||||
|
request.request('https://qt.gtimg.cn/q=s_sh000001', {
|
||||||
|
header: {
|
||||||
|
'Content-Type': 'text/html; charset=GBK'
|
||||||
|
},
|
||||||
|
expectDataType: http.HttpDataType.ARRAY_BUFFER
|
||||||
|
}).then(res => {
|
||||||
|
let data = this.gbkToUTF8(res.result as ArrayBuffer);
|
||||||
|
Log.info(res.responseCode.toString());
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gbkToUTF8(content: ArrayBuffer): string {
|
||||||
|
let textDecoderOptions: util.TextDecoderOptions = {
|
||||||
|
fatal: false,
|
||||||
|
ignoreBOM : true
|
||||||
|
}
|
||||||
|
let decodeWithStreamOptions: util.DecodeWithStreamOptions = {
|
||||||
|
stream: true
|
||||||
|
}
|
||||||
|
let textDecoder = util.TextDecoder.create('gbk', textDecoderOptions);
|
||||||
|
let retStr = textDecoder.decodeWithStream( new Uint8Array(content) , decodeWithStreamOptions);
|
||||||
|
return retStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user