电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android的簡單前后端交互(okHttp+springboot+mysql)

瀏覽:100日期:2022-09-18 18:52:31
前言

前陣子發現了個有意思又好用的框架——okHttp。由于課程設計需要,無意間發現了這個框架,打算利用此框架與后端交互,可以參考前后端分離的項目,把android當做前端,springboot當做后端,以下是二者的簡單交互。

okHttp說明

(1)android網絡框架之OKhttp

一個處理網絡請求的開源項目,是安卓端最火熱的輕量級框架,由移動支付Square公司貢獻(該公司還貢獻了Picasso)

用于替代HttpUrlConnection和Apache HttpClient

(2)okHttp優勢

允許連接到同一個主機地址的所有請求,提高請求效率

共享Socket,減少對服務器的請求次數

通過連接池,減少了請求延遲

緩存響應數據來減少重復的網絡請求

減少了對數據流量的消耗

自動處理GZip壓縮

(3)OKhttp的功能

get,post請求

文件的上傳下載

加載圖片(內部會圖片大小自動壓縮)

支持請求回調,直接返回對象、對象集合

支持session的保持

android前端

邏輯控制:LoginActivity.java

import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import com.campus.book.R;import com.campus.book.entity.User;import com.campus.book.util.http.OKHttpUtil;import com.google.gson.Gson;public class LoginActivity extends AppCompatActivity {//這個url可以通過cmd中輸入 ipconfig IPv4 地址即為本地電腦的地址 8081為后端的端口號 private String baseUrl='http://192.168.xxx.1:8081'; private TextView tv=null; EditText userId = null; EditText pwd = null ; Button login=null; private Button registry=null; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_login);setTitle('登錄');tv=findViewById(R.id.tv);login = (Button)findViewById(R.id.login);registry = (Button)findViewById(R.id.registry);userId=(EditText) findViewById(R.id.userId);pwd=findViewById(R.id.pwd);login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {String id = userId.getText().toString();String password=pwd.getText().toString();User user=new User(id,password);Gson gson=new Gson();String json=gson.toJson(user);String args[]=new String[]{'user','login'};String res= OKHttpUtil.postSyncRequest(baseUrl,json,args);Log.d('同步:',res);res= OKHttpUtil.postAsyncRequest(baseUrl,json,args);Log.d('異步:',res); }});registry.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {String args[]=new String[]{'user','getUser','123'};String res= OKHttpUtil.getSyncRequest(baseUrl,args);System.out.println('同步:'+res);String args1[]=new String[]{'user','getUser','123'}; res= OKHttpUtil.getAsyncRequest(baseUrl,args1);System.out.println('異步:'+res); }}); }}

布局方式:activity_login.xml

<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:app='http://schemas.android.com/apk/res-auto' xmlns:tools='http://schemas.android.com/tools' android:orientation='vertical' android:layout_width='match_parent' android:layout_height='match_parent' tools:context='.activity.LoginActivity'><TextView android:layout_width='match_parent' android:layout_height='wrap_content' android: android:text='內容:'/> <FrameLayoutandroid:layout_width='wrap_content'android:layout_height='wrap_content'><ImageView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:background='@drawable/login' /><!--@drawable/login改成相應的背景圖--><TableLayout android:layout_width='350dp' android:layout_height='match_parent' android:stretchColumns='*' android:layout_marginBottom='150sp' android:layout_gravity='center' > <TableRow android:layout_height='match_parent'><EditText android: android:layout_column='0' android:layout_span='2' android:hint='請輸入手機號' android:textColorHint='#FFFFFF' android:textColor='#FFFFFF' android:textCursorDrawable='@drawable/cursor_color' android:textSize='15sp' /> </TableRow> <TableRow android:layout_height='match_parent' ><EditText android: android:inputType='textPassword' android:layout_column='0' android:layout_span='2' android:hint='請輸入密碼' android:textColorHint='#FFFFFF' android:textColor='#FFFFFF' android:textCursorDrawable='@drawable/cursor_color' android:textSize='15sp' /> </TableRow> <TableRow android:layout_height='match_parent'><Button android: android:layout_height='wrap_content' android:textColor='#FFFFFF' android:background='#000000' android:layout_margin='8dp' android:textSize='15sp' android:text='登錄' /><Button android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:textColor='#FFFFFF' android:background='#000000' android:layout_margin='8dp' android:textSize='15sp' android:text='注冊' /> </TableRow></TableLayout> </FrameLayout></LinearLayout>

其中,cursor_color.xml在drawable中。

<?xml version='1.0' encoding='utf-8'?><shape xmlns:android='http://schemas.android.com/apk/res/android' android:shape='rectangle' > <size android: /> <solid android:color='@android:color/black' /></shape>springboot+mysql后端

(1)數據庫

表user

Android的簡單前后端交互(okHttp+springboot+mysql)

(2)springboot中的controller層

如何搭建springboot工程就不再贅述了(如有需要,可留言,后續可發搭建教程),可自行參考其他文章。

@RestController@RequestMapping('//user')public class UserController {@Autowiredprivate UserService userService;//Gson gson= JsonBean.getGson(); static Gson gson=new GsonBuilder().serializeNulls().create();@GetMapping('/list')public List<User> list() {return this.userService.list();}@PostMapping('/login')public User login(String json){User result=null;User user=null;User user1=null;try{user=gson.fromJson(json,User.class);}catch (Exception e){e.printStackTrace();}user1=userService.getById(user.getUserId());if(user1!=null){//存在該賬戶if(user1.getPassword().equals(user.getPassword())){//密碼正確result=user1;}else{//密碼錯誤}}else{//不存在該賬戶}return result;}@GetMapping('/getUser/{id}')public User getUser(@PathVariable('id') Serializable id){User user=userService.getById(id);if(user!=null){//存在}else{//不存在}return user;}}

運行(交互)效果

Android的簡單前后端交互(okHttp+springboot+mysql)

(1)點擊“登錄”按鈕,發起post請求

android端

Android的簡單前后端交互(okHttp+springboot+mysql)

后端

Android的簡單前后端交互(okHttp+springboot+mysql)

(2)點擊“注冊”按鈕發起get請求

android端

Android的簡單前后端交互(okHttp+springboot+mysql)

后端

Android的簡單前后端交互(okHttp+springboot+mysql)

這樣就達到了前后端分離的效果,是不是很神奇!可以愉快的和小組成員分開進行開發啦!

在Android端中用到了個人結合需要編寫的okHttp的工具類,可參考上篇文章:okHttp的get和post請求的簡單封裝與使用

到此這篇關于Android的簡單前后端交互(okHttp+springboot+mysql)的文章就介紹到這了,更多相關Android 前后端交互內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 闭端端子|弹簧螺式接线头|防水接线头|插线式接线头|端子台|电源线扣+护线套|印刷电路板型端子台|金笔电子代理商-上海拓胜电气有限公司 | 布袋式除尘器|木工除尘器|螺旋输送机|斗式提升机|刮板输送机|除尘器配件-泊头市德佳环保设备 | 球磨机,节能球磨机价格,水泥球磨机厂家,粉煤灰球磨机-吉宏机械制造有限公司 | 一体式钢筋扫描仪-楼板测厚仪-裂缝检测仪-泰仕特(北京) | 法钢特种钢材(上海)有限公司 - 耐磨钢板、高强度钢板销售加工 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 海水晶,海水素,海水晶价格-潍坊滨海经济开发区强隆海水晶厂 | 上海办公室装修_上海店铺装修公司_厂房装潢设计_办公室装修 | 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 | 阴离子_阳离子聚丙烯酰胺厂家_聚合氯化铝价格_水处理絮凝剂_巩义市江源净水材料有限公司 | 铝合金脚手架厂家-专注高空作业平台-深圳腾达安全科技 | 膏剂灌装旋盖机-眼药水灌装生产线-西林瓶粉剂分装机-南通博琅机械科技 | 硅PU球场、篮球场地面施工「水性、环保、弹性」硅PU材料生产厂家-广东中星体育公司 | 干法制粒机_智能干法制粒机_张家港市开创机械制造有限公司 | 广州二手电缆线回收,旧电缆回收,广州铜线回收-广东益福电缆线回收公司 | 袋式过滤器,自清洗过滤器,保安过滤器,篮式过滤器,气体过滤器,全自动过滤器,反冲洗过滤器,管道过滤器,无锡驰业环保科技有限公司 | 楼承板-开闭口楼承板-无锡海逵楼承板 | 数显恒温油浴-电砂浴-高温油浴振荡器-常州迈科诺仪器有限公司 | 北京中航时代-耐电压击穿试验仪厂家-电压击穿试验机 | 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 凝胶成像仪,化学发光凝胶成像系统,凝胶成像分析系统-上海培清科技有限公司 | 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | 网优资讯-为循环资源、大宗商品、工业服务提供资讯与行情分析的数据服务平台 | 黑龙江京科脑康医院-哈尔滨精神病医院哪家好_哈尔滨精神科医院排名_黑龙江精神心理病专科医院 | 番茄畅听邀请码怎么输入 - Dianw8.com | 广东青藤环境科技有限公司-水质检测 | 建筑资质代办-建筑企业资质代办机构-建筑资质代办公司 | 紫外线老化试验箱_uv紫外线老化试验箱价格|型号|厂家-正航仪器设备 | 14米地磅厂家价价格,150吨地磅厂家价格-百科| 北京浩云律师事务所-法律顾问_企业法务_律师顾问_公司顾问 | 聚丙烯酰胺_阴离子_阳离子「用量少」巩义亿腾厂家直销,售后无忧 聚合甘油__盐城市飞龙油脂有限公司 | 深圳APP开发公司_软件APP定制开发/外包制作-红匣子科技 | 酒糟烘干机-豆渣烘干机-薯渣烘干机-糟渣烘干设备厂家-焦作市真节能环保设备科技有限公司 | 经济师考试_2025中级经济师报名时间_报名入口_考试时间_华课网校经济师培训网站 | 菲希尔FISCHER测厚仪-铁素体检测仪-上海吉馨实业发展有限公司 | 矿用履带式平板车|探水钻机|气动架柱式钻机|架柱式液压回转钻机|履带式钻机-启睿探水钻机厂家 | 玉米深加工设备-玉米深加工机械-新型玉米工机械生产厂家-河南粮院机械制造有限公司 | 山东活动策划|济南活动公司|济南公关活动策划-济南锐嘉广告有限公司 | 上海办公室装修公司_办公室设计_直营办公装修-羚志悦装 | 不锈钢复合板|钛复合板|金属复合板|南钢集团安徽金元素复合材料有限公司-官网 | 合肥触摸一体机_触摸查询机厂家_合肥拼接屏-安徽迅博智能科技 | 针焰试验仪,灼热丝试验仪,漏电起痕试验仪,水平垂直燃烧试验仪 - 苏州亚诺天下仪器有限公司 |