创新实训博客(25)——接口调用请求汇总(用户基本操作和历史记录相关部分)_一位不愿意透露姓名的热心网友的博客-程序员秘密

1. Vue-用户登录、

export function login(data) {
  return request({
    url: '/user/login',
    method: 'post',
    data
  })
}

2. Vue-用户注册

export function register(data) {
  return request({
    url: '/user/regist',
    method: 'post',
    data
  })
}

3. Vue-获取用户信息

export function getInfo() {
  return request({
    url: '/user/info',
    method: 'get'
  })
}

4. Vue-更新用户信息

export function updateUserInfo(data) {
  return request({
    url: '/user/edit',
    method: 'post',
    data
  })
}

5. Vue-获取历史记录

export function getUserHistoryList(page, size) {
  return request({
    url: '/record/getBrowsingHistory',
    method: 'get',
    params: { page: page, size: size }
  })
}

6. Vue-获取点赞数量、订阅数量

export function getRecord() {
  return request({
    url: '/record/count',
    method: 'get'
  })
}

7. App-用户登录

fun onLogin(view: View) {
        // 请求地址
        val api = getString(R.string.api)
        // 获取登录的用户名和密码
        val username: EditText = findViewById(R.id.login_user)
        val passwd: EditText = findViewById(R.id.login_passwd)
        // 转化为json对象
        val obj = JSONObject()
        obj.put("username", username.text.toString())
        obj.put("password", passwd.text.toString())
        // 设置请求实例
        val okHttpClient = OkHttpClient()
        val mediaType = "application/json; charset=utf-8".toMediaType()
        val requestBody = JSON.toJSONString(obj).toRequestBody(mediaType)
        val request = Request.Builder()
            .url("$api/user/login")
//            .header("Auth", "token")
            .post(requestBody)
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                val resobj = JSON.parseObject(response.body!!.string())
                if (resobj["code"] == 200) {
                    // 登录成功
                    val newIntent = Intent([email protected], MainActivity::class.java)
                    val token = JSON.parseObject(resobj["data"].toString())["token"].toString()
                    newIntent.putExtra("token", token)
                    startActivity(newIntent)
                    // token 写入存储
                    val sharedPreferences = getSharedPreferences("data", Context.MODE_PRIVATE)
                    val editor: SharedPreferences.Editor = sharedPreferences.edit()
                    editor.putString("token", token)
                    editor.apply()
                    editor.commit()
                    [email protected]()
                } else {
                    // 登录失败提示信息
                    Looper.prepare()
                    Toast.makeText(
                        [email protected],
                        resobj["code"].toString() + "-" + resobj["msg"].toString(),
                        Toast.LENGTH_SHORT
                    ).show()
                    Looper.loop()
                }
            }
        })
    }

8. App-用户注册

fun onRegister(view: View) {
        // 请求地址
        val api = getString(R.string.api)
        // 获取登录的用户名和密码
        val username: EditText = findViewById(R.id.register_user)
        val email: EditText = findViewById(R.id.register_email)
        val passwd: EditText = findViewById(R.id.register_passwd)
        // 转化为json对象
        val obj = JSONObject()
        obj.put("username", username.text.toString())
        obj.put("email", email.text.toString())
        obj.put("password", passwd.text.toString())
        // 设置请求实例
        val okHttpClient = OkHttpClient()
        val mediaType = "application/json; charset=utf-8".toMediaType()
        val requestBody = JSON.toJSONString(obj).toRequestBody(mediaType)
        val request = Request.Builder()
            .url("$api/user/regist")
//            .header("Auth", "token")
            .post(requestBody)
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                val resobj = JSON.parseObject(response.body!!.string())
                Looper.prepare()
                Toast.makeText(
                    [email protected],
                    resobj["code"].toString() + "-" + resobj["msg"].toString(),
                    Toast.LENGTH_SHORT
                ).show()
                Looper.loop()
                if (resobj["code"] == 200) {
                    // 注册成功
                    [email protected]()
                }
            }
        })
    }

9. App-获取用户信息

fun getInfo() {
        // 请求地址
        val api = getString(R.string.api)
        // 获取token
        val token = intent.getStringExtra("token")
        // 设置请求实例
        val okHttpClient = OkHttpClient()
        val request = Request.Builder()
            .url("$api/user/info")
            .header("Authorization", token)
            .get()
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                val resobj = JSON.parseObject(response.body!!.string())
                if (resobj["code"] == 200) {
                    // 获取成功
                    dataObject = JSON.parseObject(resobj["data"].toString())
                    val message = Message()
                    message.what = 200
                    handler.sendMessage(message)
                } else {
                    // 获取失败提示信息
                    Looper.prepare()
                    Toast.makeText(
                        [email protected],
                        resobj["code"].toString() + "-" + resobj["msg"].toString(),
                        Toast.LENGTH_SHORT
                    ).show()
                    Looper.loop()
                }
            }
        })
    }

10. App-更新信息

fun onUpdate(view: View) {
        // 设置请求的对象
        val id: TextView = findViewById(R.id.user_info_id)
        val username: TextView = findViewById(R.id.user_info_name)
        val role: TextView = findViewById(R.id.user_info_role)
        val email: EditText = findViewById(R.id.user_info_email)
        val mobile: EditText = findViewById(R.id.user_info_mobile)
        val sex: EditText = findViewById(R.id.user_info_sex)
        val age: EditText = findViewById(R.id.user_info_age)
        val job: EditText = findViewById(R.id.user_info_job)
        val school: EditText = findViewById(R.id.user_info_school)
        val obj = JSONObject()
        obj.put("id", id.text.toString().toInt())
        obj.put("name", username.text.toString())
        if (role.text.toString() == "普通用户") {
            obj.put("role", 0)
        } else if (role.text.toString() == "管理员") {
            obj.put("role", 1)
        }
        obj.put("email", email.text.toString())
        obj.put("mobile", mobile.text.toString())
        obj.put("sex", sex.text.toString())
        obj.put("age", age.text.toString().toInt())
        obj.put("job", job.text.toString())
        obj.put("school", school.text.toString())
        // 请求地址
        val api = getString(R.string.api)
        // 获取token
        val token = intent.getStringExtra("token")
        // 设置请求实例
        val okHttpClient = OkHttpClient()
        val mediaType = "application/json; charset=utf-8".toMediaType()
        val requestBody = JSON.toJSONString(obj).toRequestBody(mediaType)
        val request = Request.Builder()
            .url("$api/user/edit")
            .header("Authorization", token)
            .post(requestBody)
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                val resobj = JSON.parseObject(response.body!!.string())
                Looper.prepare()
                Toast.makeText(
                    [email protected],
                    resobj["code"].toString() + "-" + resobj["msg"].toString(),
                    Toast.LENGTH_SHORT
                ).show()
                Looper.loop()
            }
        })
    }

11. App-获取点赞历史

fun fetchLikeList() {
        // 请求地址
        val api = getString(R.string.api)
        // 返回列表
        val list = LinkedList<RecentItem>()
        // 获取token
        val token = intent.getStringExtra("token")
        // 设置请求实例
        val okHttpClient = OkHttpClient()
        val request = Request.Builder()
            .url("$api/record/getLikingList?page=1&size=30")
            .header("Authorization", token)
            .get()
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                val resobj = JSON.parseObject(response.body!!.string())
                if (resobj["code"] == 200) {
                    // 获取成功
                    val dataArray = JSON.parseArray(resobj["data"].toString())
                    for (i in dataArray.indices) {
                        val item = JSON.parseObject(dataArray[i].toString())
                        val id = item["id"].toString().toInt()
                        val tag = item["author"].toString()
                        val title = item["title"].toString()
                        list.add(RecentItem(1, id, title, tag, ""))
                    }
                    val message = Message()
                    message.what = 2
                    globalList = list
                    handler.sendMessage(message)
                } else {
                    // 获取失败提示信息
                    Looper.prepare()
                    Toast.makeText(
                        [email protected],
                        resobj["code"].toString() + "-" + resobj["msg"].toString(),
                        Toast.LENGTH_SHORT
                    ).show()
                    Looper.loop()
                }
            }
        })
    }

12. App-获取浏览历史

fun fetchHisList() {
        // 请求地址
        val api = getString(R.string.api)
        // 返回列表
        val list = LinkedList<RecentItem>()
        // 获取token
        val token = intent.getStringExtra("token")
        // 设置请求实例
        val okHttpClient = OkHttpClient()
        val request = Request.Builder()
            .url("$api/record/getBrowsingHistory?page=1&size=30")
            .header("Authorization", token)
            .get()
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                val resobj = JSON.parseObject(response.body!!.string())
                if (resobj["code"] == 200) {
                    // 获取成功
                    val dataArray = JSON.parseArray(resobj["data"].toString())
                    for (i in dataArray.indices) {
                        val item = JSON.parseObject(dataArray[i].toString())
                        val id = item["id"].toString().toInt()
                        val tag = item["author"].toString()
                        val title = item["title"].toString()
                        list.add(RecentItem(2, id, title, tag, ""))
                    }
                    val message = Message()
                    message.what = 3
                    globalList = list
                    handler.sendMessage(message)
                } else {
                    // 获取失败提示信息
                    Looper.prepare()
                    Toast.makeText(
                        [email protected],
                        resobj["code"].toString() + "-" + resobj["msg"].toString(),
                        Toast.LENGTH_SHORT
                    ).show()
                    Looper.loop()
                }
            }
        })
    }

13. App-获取订阅列表

fun fetchTagList() {
        // 请求地址
        val api = getString(R.string.api)
        // 返回列表
        val list = LinkedList<TagItem>()
        // 获取token
        val token = intent.getStringExtra("token")
        // 设置请求实例
        val okHttpClient = OkHttpClient()
        val request = Request.Builder()
            .url("$api/subscription/list")
            .header("Authorization", token)
            .get()
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                val resobj = JSON.parseObject(response.body!!.string())
                if (resobj["code"] == 200) {
                    // 获取成功
                    val dataArray = JSON.parseArray(resobj["data"].toString())
                    for (i in dataArray.indices) {
                        val item = JSON.parseObject(dataArray[i].toString())
                        val id = item["id"].toString().toInt()
                        val type = item["type"].toString().toInt()
                        if(type == 0){
                            val name = item["title"].toString()
                            val tag = item["url"].toString()
                            list.add(TagItem(11, id, name, 0, tag))

                        }else if(type == 1) {
                            val name = item["name"].toString()
                            val count = item["count"].toString().toInt()
                            list.add(TagItem(0, id, name, count, ""))
                        }
                    }
                    val message = Message()
                    message.what = 1
                    globalList2 = list
                    handler.sendMessage(message)
                } else {
                    // 获取失败提示信息
                    Looper.prepare()
                    Toast.makeText(
                        [email protected],
                        resobj["code"].toString() + "-" + resobj["msg"].toString(),
                        Toast.LENGTH_SHORT
                    ).show()
                    Looper.loop()
                }
            }
        })
    }

14. App-获取点赞数量等信息

fun fetchCountTotal() {
        // 请求地址
        val api = getString(R.string.api)
        // 获取token
        val token = arguments!!.getString("token").toString()
        // 设置请求实例
        val okHttpClient = OkHttpClient()
        val request = Request.Builder()
            .url("$api/record/count")
            .header("Authorization", token)
            .get()
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                e.printStackTrace()
            }

            override fun onResponse(call: Call, response: Response) {
                val resobj = JSON.parseObject(response.body!!.string())
                if (resobj["code"] == 200) {
                    // 获取成功
                    val subView:TextView = activity!!.findViewById(R.id.user_frg_data_1)
                    val likeView:TextView = activity!!.findViewById(R.id.user_frg_data_2)
                    val readView:TextView = activity!!.findViewById(R.id.user_frg_data_3)
                    // 设置
                    val dataObj = JSON.parseObject(resobj["data"].toString())
                    subView.text = dataObj["subCount"].toString()
                    likeView.text = dataObj["likingCount"].toString()
                    readView.text = dataObj["browsingCount"].toString()
                } else {
                    // 获取失败提示信息
                    Looper.prepare()
                    Toast.makeText(
                        activity,
                        resobj["code"].toString() + "-" + resobj["msg"].toString(),
                        Toast.LENGTH_SHORT
                    ).show()
                    Looper.loop()
                }
            }
        })
    }

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sinat_41868837/article/details/106882493

智能推荐

redis-cluster学习总结_rediscluster.incr_baidu_38176716的博客-程序员秘密

redis-cluster 教程拿台球项目来说,我要把用户的一些信息写入redis缓存中,由于redis存储方式是key-value。我们就存呗,谁怕谁啊。import com.yuyuka.billiards.service.config.DiamondConfigurtion;import com.yuyuka.billiards.service.service.lock.JedisLo...

7-1 旅游规划 (25分)_ 蓝的博客-程序员秘密

有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。输入格式:输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中

Oracle中判断select的字段值为NULL的函数_oracle select null_zdleek的博客-程序员秘密

COALESCE()这个函数是ASNI标准的SQL函数,MS SQL 和 oracle都可以用NVL()是oracel独有的函数,功能与COALESCE()相同而在 MS SQL中的ISNULL()函数功能与COALESCE()相同1  SELECT NV

Linux常用命令-文本处理_baiju1963的博客-程序员秘密

Linux文本处理工具  文本处理工具文件内容: less 和cat文件截取:head和tail按列抽取:cut按关键字抽取:grep  文件查看 catUsage: cat [OPTION]... [FILE]...-E 显示行的结束符号$-n 显示每一行的行号-b 给非空行编号-s 折叠空行为一行  tac 倒序显示...

Built-in转URP Chapter9(4)-透明度测试阴影_懂装修的程序员的博客-程序员秘密

话不多说,直接上代码,具体区别我后边会补充上,暂时委屈各位,先锻炼下自己的“找不同”的能力,哈哈:Built-CG写法:Shader "Unity Shaders Book/Chapter 9/Alpha Test With Shadow" { Properties { _Color("Color Tint", Color) = (1,1,1,1) _MainTex("Main Tex", 2D) = "white" {} _Cutoff("Alpha Cutoff", Rang

介绍Jackson JsonParser解析json_梦想画家的博客-程序员秘密

介绍Jackson JsonParser解析json前文介绍了通过JsonNode解析json,本文深入底层工具JsonParser的用法。1. 概述实际应用中经常需要解析json数据,如查询NoSql数据库时响应数据格式通常为Json格式。JsonNode可以非常方便地实现,JsonParser类是底层JSon解析器,类似于Java使用stAx解析xml,但JsonParser仅仅解析Js...

随便推点

Android 广播接收者 短信拦截_weixin_30333885的博客-程序员秘密

定义广播接收者1.定义类继承BroadcastReceiver,重写onReceive方法2.当接收到匹配广播之后就会执行onReceive方法3.清单文件中声明&lt;receiver&gt;,需要在其中配置&lt;intent-filter&gt;指定接收广播的动作和类型4.BroadcastReceiver除了在清单文件中声明,也可以在代码中声明,使用registe...

刷卡门禁或PN532刷卡门禁_pn532可以ttl吗_1029179954的博客-程序员秘密

1、材料准备(1)PN532(2)usb转ttl(串口通信)(3)继电器(4)电磁铁(5)安卓线2、电路连接这个比较简单,将硬件的针脚与树莓派一一对应就好了。整体的链接就是PN532链接usb转ttl和USB转ttl链接树莓派。连接图3、代码()ser = serial.Serial("/dev/ttyUSB0", 115200)# *****************...

jdk8新特(lombda篇)_JAVA攻城狮_0903的博客-程序员秘密

看代码和注释lombda类似于es6中的写法package cy.learningInterface;/** * 作者:cy * 时间:2021/4/29 * lombda ---&gt; jdk8新特性,简化代码,挺高效率,增加美观度 ---&gt; 用于在匿名函数 * 注意:并不是所有的接口都可以使用lombda表达式的,要求接口中定义的必须要实现的抽象方法只能是一个(不能多也不能少) 可以在接口类上写上注解(@FunctionalInterface) * @FunctionalInt

pyecharts绘制地铁图_2025年北京市轨道交通线路预绘图(含地铁、有轨电车、市郊铁路)..._weixin_39624097的博客-程序员秘密

我觉得我是一个非常没有理科细胞的人。所以一直以来就算非常喜欢轨道交通却也从未尝试用电脑进行任意一张专业的设计图绘制。这是我第一次使用Adobe Illustrator,大概用了我六天的时间,还算满意。市面上现在能看到的地铁线路图,无论是官方的,还是地铁迷与设计迷自绘的诸多版本,多为优化后的走向,看起来相对直观,但却失真,无法让我们准确判断两地距离。因此我才绘了这一版即为真实比例和走向,又看起来相对...

前端项目node-sass安装报错的解决方案_柯晓楠的博客-程序员秘密

前端项目中使用了 sass ,在使用 npm install 命令初始化依赖时经常会报 node-sass 安装失败的错误。报错的原因有以下几种:node-sass需要Python环境,本地没有安装Python环境;本地安装Python环境,但是版本高于Python2;本地没有Python环境,但是也没有其他的编译工具,例如 node-gyp ;上面只一部分引起 node-sass 报错的原因,如果已经有编译环境还报错的话,有可能是因为npm源镜像的问题。解决方案:安装 Python2 环

iptables命令_zm_21的博客-程序员秘密

常用命令显示filter表中的INPUT链的所有规则sudo iptables -t filter -L INPUT-t filter可以去掉显示filter表中的所有规则链sudo iptables -L删除filter表中所有的规则sudo iptables -F删除filter表中用户定义的规则链sudo iptables -X

推荐文章

热门文章

相关标签