Android---SpringBoot实现前后端数据交互

Android---SpringBoot实现前后端数据交互,第1张

Android—SpringBoot实现前后端数据交互

星光不问赶路人,时间不负有心人

这篇是针对android传数据到后台springboot,使用Xutils框架

使用Xutils框架 关于xutils的使用这是老师的博客大家可以看看

文章目录 Android---SpringBoot实现前后端数据交互前端后端数据交互方式SpringBoot获取参数的几种常用注解`get请求``post请求`后台实体对象接收key-value接收map接收

前端后端数据交互方式

前端后端数据提交格式

SpringBoot获取参数的几种常用注解

@PathVariable:一般我们使用URI template样式映射使用,即url/{param}这种形式,也就是一般我们使用的GET,DELETE,PUT方法会使用到的,我们可以获取URL后所跟的参数。

@RequestParam:一般我们使用该注解来获取多个参数,在()内写入需要获取参数的参数名即可,一般在PUT,POST中比较常用。

@RequestBody:该注解和@RequestParam殊途同归,我们使用该注解将所有参数转换,在代码部分在一个个取出来,也是目前我使用到最多的注解来获取参数

get请求

params传参一般用于get请求,params传参时参数会附于uri后面以问号形式展示,比如

http://localhost/login?username=xiaoming&password=123456

这种就是get请求常见格式,在地址栏输入http://localhost/login,然后选params再输入key和value,然后问号形式展现的参数就会被自动加到地址最后。

一般用于查询数据,采用明文进行传输,一般用来获取一些无关用户信息的数据,

@GetMapping 组合注解,是 @RequestMapping(method = RequestMethod.GET) 的缩写

get请求,url路径传参
get请求一般通过url传参,如:
http://10.138.164.148:9000/get/21
后端要获取name参数,可以使用@RequestParam注解
后端使用@PathVariable可以接收路径参数21。

Android代码:

RequestParams requestParams = new RequestParams("http://10.138.164.148:9000/get/21");
requestParams.addQueryStringParameter("name","suke");

SpringBoot代码:

 @GetMapping("/get/{id}")
    public void getTest(@PathVariable (name = "id") int id, @RequestParam (name = "name") String pname){
        System.out.println("id = " + id);
        System.out.println("pname = " + pname);
    }

当请求为get请求时,使用@PathVariable或者@RequestParam获取参数值,获取路径参数。@PathVariable一般用于获取获取*url/{id}这种形式的参数;@RequestParam获取查询参数。即url?name=*这种形式

post请求

body传参一般用于post请求,body传参时需要在body里写json数组,参数不会显示在地址栏中
ps:post请求一般用来传登录时的账号密码,以json数组的形式传给后台

后台实体对象接收

较推荐使用json格式传值

Android代码:

RequestParams requestParams = new RequestParams("http://10.138.164.148:9000/post");
        Student qifei = new Student(44, "qifei", 20);
        Gson gson = new Gson();
        String jsonStudent = gson.toJson(qifei);
        requestParams.setAsJsonContent(true);  //  设置传输格式为json
        requestParams.setBodyContent(jsonStudent);

SpringBoot代码:

@PostMapping("/post")
    public String postTest(@RequestBody Student student){
        System.out.println(student.toString()+"========");
        return student.toString();
    }

key-value接收

Android代码:

RequestParams requestParams = new RequestParams("http://10.138.164.148:9000/postq");
        requestParams.addBodyParameter("name","qifei");
        requestParams.addBodyParameter("age","18");

SpringBoot代码:

 @PostMapping("/postq")
    public void postTest1(@RequestParam (name = "name") String pname , @RequestParam (name = "age") String age){
        System.out.println("pname = " + pname);
        System.out.println("age = " + age);
        return ;
    }

map接收

Android代码:

/*
        post请求 ==> map接受 */
        RequestParams requestParams = new RequestParams("http://10.138.164.148:9000/post3");   //json对象传值
        Student qifei = new Student(44, "qifei", 20);
        Gson gson = new Gson();
        String jsonStudent = gson.toJson(qifei);
        requestParams.setAsJsonContent(true);
        requestParams.setBodyContent(jsonStudent);

SpringBoot代码:

@PostMapping("/post3")
    public String postTest3(@RequestBody Map<String,String> mp){
        System.out.println(mp.get("name") + mp.get("age"));
        return mp.get("name")+mp.get("age");
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/992131.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存