1.后端比較熟悉,先說(shuō)后端框架搭建
(1)創(chuàng)建項(xiàng)目,總體框架
創(chuàng)建項(xiàng)目后,自己添加包,框架如下
?
userController里的一些內(nèi)容,只供參考,代碼不全,無(wú)法實(shí)現(xiàn)
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 用戶登錄類(lèi)
*/
@PostMapping("/login")
public R<User> login(@RequestBody User user, HttpSession session){
// 查詢數(shù)據(jù)庫(kù)
final LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getUserName, user.getUserName());
final User user1 = userService.getOne(queryWrapper);
// 沒(méi)有根據(jù)賬號(hào)找到數(shù)據(jù)
if (user1 == null) {
return R.error("賬號(hào)不存在");
}
// 密碼比對(duì)
if (!user1.getUserPassword().equals(user.getUserPassword())) {
return R.error("密碼錯(cuò)誤");
}
// 登陸成功,保存userID
session.setAttribute("userName", user.getUserName());
log.info("當(dāng)前登錄用戶name:" + user.getUserName());
return R.success(user1);
}
}
(2)創(chuàng)建數(shù)據(jù)庫(kù)
數(shù)據(jù)庫(kù)是直接在社區(qū)版IDEA里連接Mysql,在控制臺(tái)端創(chuàng)建的數(shù)據(jù)庫(kù)和user表,用于數(shù)據(jù)交互。
2.Android前端
(1)前端主要框架
Activity包里是Activity Java類(lèi),主要響應(yīng)layout包里activity_login.xml等頁(yè)面布局內(nèi)的按鈕響應(yīng)
(2)寫(xiě)登錄功能
activity_login.xml里的內(nèi)容,只參考界面布局代碼即可,代碼不可用
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="bottom|center"
android:orientation="vertical"
tools:context=".Activity.LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="122dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="bottom|center"
android:paddingBottom="15dp"
android:text="登錄"
android:textColor="#0E0D0B"
android:textSize="34sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/Username"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="20dp"
android:ems="10"
android:gravity="bottom|center"
android:hint="請(qǐng)輸入用戶名"
android:inputType="textPersonName"
android:paddingLeft="20dp"
android:textSize="24sp" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="25dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="20dp"
android:ems="10"
android:gravity="bottom|center"
android:hint="請(qǐng)輸入密碼"
android:inputType="textPassword"
android:paddingLeft="20dp"
android:textSize="24sp"
android:visibility="visible" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/login"
android:layout_width="320dp"
android:layout_height="75dp"
android:layout_gravity="bottom|center"
android:layout_marginTop="30dp"
android:text="登錄"
android:textSize="24sp" />
<Button
android:id="@+id/register"
android:layout_width="320dp"
android:layout_height="75dp"
android:layout_gravity="bottom|center"
android:layout_marginTop="30dp"
android:text="注冊(cè)"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
private EditText name;
private EditText password;
Button login,register; //登錄、注冊(cè)按鈕
@Override
protected void onCreate(Bundle savedState){
super.onCreate(savedState);
setContentView(R.layout.activity_login);
name = findViewById(R.id.Username); //獲取輸入的賬號(hào)
password = findViewById(R.id.password); //獲取輸入的密碼
login = findViewById(R.id.login);
register = findViewById(R.id.register);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = "http://后端部署ip地址:8080/user/login";
String username = name.getText().toString().trim();
String userPassword = password.getText().toString().trim();
if(username.equals("")){
Toast.makeText(LoginActivity.this, "請(qǐng)輸入用戶名", Toast.LENGTH_SHORT).show();
}else if(userPassword.equals("")){
Toast.makeText(LoginActivity.this, "請(qǐng)輸入密碼", Toast.LENGTH_SHORT).show();
}else {
//請(qǐng)求傳入的參數(shù)
JSONObject user = new JSONObject();
try{
user.put("userName",username);
user.put("userPassword",userPassword);
}catch (JSONException e){
e.printStackTrace();
}
OkHttpClient httpClient = new OkHttpClient();
MediaType type = MediaType.parse("application/json;charset=utf-8");
RequestBody requestBody = RequestBody.create(type,""+ user);
Request getRequest = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Call call = httpClient.newCall(getRequest);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i(TAG, "post請(qǐng)求失敗 \n" +
"*********請(qǐng)求體,傳送數(shù)據(jù)*********** \n"+
requestBody.toString() + "\n"+
"*****user里的數(shù)據(jù)***** \n"+
user);
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
assert response.body() != null;
String R = response.body().string();
Log.i(TAG, "okHttpPost enqueue: \n " +
"onResponse:"+ response.toString() +"\n " +
"body:" +R);
//將resoust轉(zhuǎn)換成jsonPath 格式
// io.restassured.path.json.JsonPath jsonPath =io.restassured.path.json.JsonPath.from(R);
try {
JSONObject toJsonObj= new JSONObject(R);
if(response.code()==200 && toJsonObj.get("code").equals(1)){
Intent intent = new Intent();
intent.setClass(LoginActivity.this, MainActivity.class);
startActivity(intent);
}
else {
Looper.prepare();
Toast.makeText(LoginActivity.this, toJsonObj.get("code")+"****"+toJsonObj.get("msg").toString(), Toast.LENGTH_SHORT).show();
Looper.loop();
}
} catch (JSONException e) {
throw new RuntimeException(e);
}
}
});
}
}
});
}
}
3.注意踩坑
(1)后端數(shù)據(jù)庫(kù)配置
建議后端代碼運(yùn)行時(shí),確認(rèn)數(shù)據(jù)庫(kù)是否能正常連接,不只看IDEA連接數(shù)據(jù)庫(kù),更要看application.yml配置文件里的數(shù)據(jù)庫(kù)
(2)Android傳數(shù)據(jù)時(shí)使用的URL
建議查看后端運(yùn)行的所在網(wǎng)絡(luò)IPV4地址,即為url請(qǐng)求地址。我在本機(jī)上運(yùn)行的后端,但是有時(shí)候電腦聯(lián)網(wǎng)不同,IPV4地址也不同,url會(huì)變,不一致則出現(xiàn)請(qǐng)求失敗的情況。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-517669.html
(3)Android okhttp3網(wǎng)絡(luò)請(qǐng)求框架
建議初學(xué)者仔細(xì)閱讀官方文檔和教程,不要在網(wǎng)上隨意找個(gè)代碼,大家的代碼思路不太相同,復(fù)制別人的代碼,但是又搞不懂請(qǐng)求流程,反而耗費(fèi)時(shí)間文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-517669.html
到了這里,關(guān)于Android前端+Spring Boot后端 登錄功能實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!