创新实训博客(25)——接口调用请求汇总(用户基本操作和历史记录相关部分)
2020-07-14 06:21
477 查看
1. Vue-用户登录、
[code]export function login(data) { return request({ url: '/user/login', method: 'post', data }) }
2. Vue-用户注册
[code]export function register(data) { return request({ url: '/user/regist', method: 'post', data }) }
3. Vue-获取用户信息
[code]export function getInfo() { return request({ url: '/user/info', method: 'get' }) }
4. Vue-更新用户信息
[code]export function updateUserInfo(data) { return request({ url: '/user/edit', method: 'post', data }) }
5. Vue-获取历史记录
[code]export function getUserHistoryList(page, size) { return request({ url: '/record/getBrowsingHistory', method: 'get', params: { page: page, size: size } }) }
6. Vue-获取点赞数量、订阅数量
[code]export function getRecord() { return request({ url: '/record/count', method: 'get' }) }
7. App-用户登录
[code]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(this@LoginActivity, 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() this@LoginActivity.finish() } else { // 登录失败提示信息 Looper.prepare() Toast.makeText( this@LoginActivity, resobj["code"].toString() + "-" + resobj["msg"].toString(), Toast.LENGTH_SHORT ).show() Looper.loop() } } }) }
8. App-用户注册
[code]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( this@RegisterActivity, resobj["code"].toString() + "-" + resobj["msg"].toString(), Toast.LENGTH_SHORT ).show() Looper.loop() if (resobj["code"] == 200) { // 注册成功 this@RegisterActivity.finish() } } }) }
9. App-获取用户信息
[code]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( this@UserInfoActivity, resobj["code"].toString() + "-" + resobj["msg"].toString(), Toast.LENGTH_SHORT ).show() Looper.loop() } } }) }
10. App-更新信息
[code]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( this@UserInfoActivity, resobj["code"].toString() + "-" + resobj["msg"].toString(), Toast.LENGTH_SHORT ).show() Looper.loop() } }) }
11. App-获取点赞历史
[code]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( this@ListActivity, resobj["code"].toString() + "-" + resobj["msg"].toString(), Toast.LENGTH_SHORT ).show() Looper.loop() } } }) }
12. App-获取浏览历史
[code]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( this@ListActivity, resobj["code"].toString() + "-" + resobj["msg"].toString(), Toast.LENGTH_SHORT ).show() Looper.loop() } } }) }
13. App-获取订阅列表
[code]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( this@ListActivity, resobj["code"].toString() + "-" + resobj["msg"].toString(), Toast.LENGTH_SHORT ).show() Looper.loop() } } }) }
14. App-获取点赞数量等信息
[code]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() } } }) }
相关文章推荐
- 创新实训博客(28)——接口调用请求汇总(App和博客相关部分)
- 创新实训博客(27)——接口调用请求汇总(博客相关部分)
- 创新实训博客(26)——接口调用请求汇总(后台管理部分)
- 创新实训博客(29)——接口调用请求汇总(可视化部分)
- 创新实训博客(32)——用户画像部分基本统计数据的设计构想
- Linux用户的基本操作1 用户相关信息 及useradd /usermod 部分
- 创新实训博客(34)——用户画像部分前端展示
- 通过ip记录用户操作历史命令
- Linux 历史命令加时间戳,message记录用户的操作时间和命令
- oracle数据库部分基本操作(表数据恢复,创建用户并加权限,数据导入导出)
- SpringCloud系列:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)
- 利用Rsyslog集中收集系统日志和用户操作记录以及相关处理方法
- git学习【7】历史记录相关操作
- 模态窗口其实就是在当前窗口调用系统的消息循环,响应用户的操作,将相关的消息发送到对应的窗口(转)
- Linux下查看/管理当前登录用户及用户操作历史记录
- linux基本操作(1)--文件、目录、用户、权限相关
- Linux下查看/管理当前登录用户及用户操作历史记录
- oracle 表空间、用户等相关基本操作
- SpringCloud系列六:Feign接口转换调用服务(Feign 基本使用、Feign 相关配置)
- 调用CSDN的开发API——>博客接口——>获取博主基本信息【2】