注意,鸿蒙中预览器preview是可以使用localhost:8081这样的本地网络地址的,但是模拟器中却不能使用,需要改成计算机地址,也就是使用ipconfig命令获取到的本机,在本地网络中的IP地址。
ArkTs中:
请求头设置为'content-type': "application/json;charset=utf-8",是为了与服务器的接收数据类型相符合。
public static async getData()
{
/*数据初始化*/
let url="http://localhost:8081/get_liaotian_value";
let in_post=http.createHttp();
let response= await in_post.request(
url,
{
method: http.RequestMethod.POST,
header: { 'content-type': "application/json;charset=utf-8" },
extraData: {}
}
);
return response.result.toString()
}
服务器中:
@PostMapping("/get_liaotian_value")
public ArrayList<Liaotian> get_liaotian_value()
{
liaotian=new ArrayList<>();
System.out.println("请求触达");
try {
BufferedReader br=new BufferedReader(new FileReader("D:/liaotian.txt"));
String l;
while ((l=br.readLine())!=null)
{
String[] c=l.split("&");
liaotian.add(new Liaotian(c[0],c[1],c[2],c[3]));
}
return liaotian;
} catch (IOException e) {
throw new RuntimeException(e);
}
}