最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年05月06日 10:57:46   作者:會(huì)飛的種花家  
這篇文章主要介紹了Android的簡(jiǎn)單前后端交互(okHttp+springboot+mysql),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

前陣子發(fā)現(xiàn)了個(gè)有意思又好用的框架——okHttp。由于課程設(shè)計(jì)需要,無(wú)意間發(fā)現(xiàn)了這個(gè)框架,打算利用此框架與后端交互,可以參考前后端分離的項(xiàng)目,把a(bǔ)ndroid當(dāng)做前端,springboot當(dāng)做后端,以下是二者的簡(jiǎn)單交互。

okHttp說(shuō)明

(1)android網(wǎng)絡(luò)框架之OKhttp

一個(gè)處理網(wǎng)絡(luò)請(qǐng)求的開(kāi)源項(xiàng)目,是安卓端最火熱的輕量級(jí)框架,由移動(dòng)支付Square公司貢獻(xiàn)(該公司還貢獻(xiàn)了Picasso)

用于替代HttpUrlConnection和Apache HttpClient

(2)okHttp優(yōu)勢(shì)

允許連接到同一個(gè)主機(jī)地址的所有請(qǐng)求,提高請(qǐng)求效率

共享Socket,減少對(duì)服務(wù)器的請(qǐng)求次數(shù)

通過(guò)連接池,減少了請(qǐng)求延遲

緩存響應(yīng)數(shù)據(jù)來(lái)減少重復(fù)的網(wǎng)絡(luò)請(qǐng)求

減少了對(duì)數(shù)據(jù)流量的消耗

自動(dòng)處理GZip壓縮

(3)OKhttp的功能

get,post請(qǐng)求

文件的上傳下載

加載圖片(內(nèi)部會(huì)圖片大小自動(dòng)壓縮)

支持請(qǐng)求回調(diào),直接返回對(duì)象、對(duì)象集合

支持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 {
//這個(gè)url可以通過(guò)cmd中輸入    ipconfig   IPv4 地址即為本地電腦的地址   8081為后端的端口號(hào)
    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:id="@+id/tv"
    android:text="內(nèi)容:"/>
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/mainBg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/login" />
<!--@drawable/login改成相應(yīng)的背景圖-->
        <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:id="@+id/userId"
                    android:layout_column="0"
                    android:layout_span="2"
                    android:hint="請(qǐng)輸入手機(jī)號(hào)"
                    android:textColorHint="#FFFFFF"
                    android:textColor="#FFFFFF"
                    android:textCursorDrawable="@drawable/cursor_color"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow android:layout_height="match_parent" >
                <EditText
                    android:id="@+id/pwd"
                    android:inputType="textPassword"
                    android:layout_column="0"
                    android:layout_span="2"
                    android:hint="請(qǐng)輸入密碼"
                    android:textColorHint="#FFFFFF"
                    android:textColor="#FFFFFF"
                    android:textCursorDrawable="@drawable/cursor_color"
                    android:textSize="15sp" />
            </TableRow>

            <TableRow android:layout_height="match_parent">
                <Button
                    android:id="@+id/login"
                    android:layout_height="wrap_content"
                    android:textColor="#FFFFFF"
                    android:background="#000000"
                    android:layout_margin="8dp"
                    android:textSize="15sp"
                    android:text="登錄" />
                <Button
                    android:id="@+id/registry"
                    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="注冊(cè)" />
            </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:width="2dp" />
    <solid android:color="@android:color/black" />
</shape>

springboot+mysql后端

(1)數(shù)據(jù)庫(kù)

表user

在這里插入圖片描述

(2)springboot中的controller層

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

@RestController
@RequestMapping("http://user")
public class UserController {
	@Autowired
	private 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){//存在該賬戶(hù)
			if(user1.getPassword().equals(user.getPassword())){//密碼正確
				result=user1;
			}else{//密碼錯(cuò)誤

			}
		}else{//不存在該賬戶(hù)

		}
		return result;
	}

	@GetMapping("/getUser/{id}")
	public User getUser(@PathVariable("id") Serializable id){
		User user=userService.getById(id);
		if(user!=null){//存在

		}else{//不存在

		}
		return user;
	}
}

運(yùn)行(交互)效果

在這里插入圖片描述

(1)點(diǎn)擊“登錄”按鈕,發(fā)起post請(qǐng)求

android端

在這里插入圖片描述

后端

在這里插入圖片描述

(2)點(diǎn)擊“注冊(cè)”按鈕發(fā)起get請(qǐng)求

android端

在這里插入圖片描述

后端

在這里插入圖片描述

這樣就達(dá)到了前后端分離的效果,是不是很神奇!可以愉快的和小組成員分開(kāi)進(jìn)行開(kāi)發(fā)啦!

在Android端中用到了個(gè)人結(jié)合需要編寫(xiě)的okHttp的工具類(lèi),可參考上篇文章:okHttp的get和post請(qǐng)求的簡(jiǎn)單封裝與使用

到此這篇關(guān)于Android的簡(jiǎn)單前后端交互(okHttp+springboot+mysql)的文章就介紹到這了,更多相關(guān)Android 前后端交互內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android自帶API實(shí)現(xiàn)分享功能

    Android自帶API實(shí)現(xiàn)分享功能

    這篇文章主要為大家詳細(xì)介紹了Android自帶API實(shí)現(xiàn)分享功能,實(shí)現(xiàn)文字和圖片的分享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android實(shí)現(xiàn)自定義加載框的代碼示例

    Android實(shí)現(xiàn)自定義加載框的代碼示例

    本篇文章主要介紹了Android實(shí)現(xiàn)自定義加載框的代碼示例,App在與服務(wù)器進(jìn)行網(wǎng)絡(luò)交互的時(shí)候,有個(gè)提示加載框,有興趣的可以了解一下。
    2017-02-02
  • android實(shí)現(xiàn)接通和掛斷電話

    android實(shí)現(xiàn)接通和掛斷電話

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)接通和掛斷電話功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android中使用開(kāi)源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互

    Android中使用開(kāi)源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互

    本文主要介紹了Android中使用開(kāi)源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • Android使用AudioRecord實(shí)現(xiàn)錄音功能

    Android使用AudioRecord實(shí)現(xiàn)錄音功能

    這篇文章主要為大家詳細(xì)介紹了Android使用AudioRecord實(shí)現(xiàn)錄音功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android 隱藏底部虛擬鍵的兩種方法

    Android 隱藏底部虛擬鍵的兩種方法

    本文通過(guò)實(shí)例代碼給大家講解了Android 隱藏底部虛擬鍵的兩種方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • android實(shí)現(xiàn)簡(jiǎn)單左滑刪除控件

    android實(shí)現(xiàn)簡(jiǎn)單左滑刪除控件

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)一個(gè)簡(jiǎn)單左滑刪除控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Kotlin中Stack與LinkedList的實(shí)現(xiàn)方法示例

    Kotlin中Stack與LinkedList的實(shí)現(xiàn)方法示例

    這篇文章主要給大家介紹了關(guān)于Kotlin中Stack與LinkedList實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-06-06
  • 簡(jiǎn)析Android五大布局(LinearLayout、FrameLayout、RelativeLayout等)

    簡(jiǎn)析Android五大布局(LinearLayout、FrameLayout、RelativeLayout等)

    這篇文章主要為大家簡(jiǎn)單分析了Android五大布局,內(nèi)容有LinearLayout、FrameLayout、RelativeLayout、AbsoluteLayout和TableLayout的相關(guān)資料,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 獲取android4.0版本sdcard路徑示例

    獲取android4.0版本sdcard路徑示例

    自從android4.0開(kāi)始,谷歌為沒(méi)有內(nèi)存卡的手機(jī)模擬了一個(gè)SD卡,占用了原來(lái)的SD卡路徑,并為真實(shí)的sd卡掛載到該目錄的子目錄,由于所掛載的目錄并沒(méi)有官方規(guī)范,所以命名會(huì)不同,只能通過(guò)搜索,下面是獲取android4.0版本sdcard路徑示例
    2014-03-03

最新評(píng)論

文登市| 集贤县| 建昌县| 札达县| 化德县| 南阳市| 舟山市| 冕宁县| 黑水县| 时尚| 枣阳市| 思南县| 静乐县| 门源| 台山市| 孟州市| 朝阳县| 罗江县| 康定县| 阜南县| 弥勒县| 临沂市| 庄浪县| 儋州市| 平度市| 嵊泗县| 汉中市| 济宁市| 策勒县| 治县。| 隆德县| 台南县| 临湘市| 迭部县| 青浦区| 易门县| 宝山区| 龙岩市| 宁强县| 罗平县| 汝阳县|