一、实现效果:
-
mysql数据库表里的情况: -
安卓可以‘user_table’中的数据,注册新增用户,用户登陆。【写的很简单】
二、提前需要:
- springboot+mysql,连接好。
- spingboot中分别写get(带参数、不带参数)和post(带参数)三个接口
- 查询到本机的IP地址
三、代码
1. 安卓部分
1. build.gradle
implementation 'com.squareup.okhttp3:okhttp:4.4.1'
implementation 'com.google.code.gson:gson:2.8.5'
2. 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LogiActivity">
<Button
android:id="@+id/button"
android:onClick="getRequest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询所有用户" />
<Button
android:id="@+id/button3"
android:onClick="getRequestParam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册" />
<Button
android:id="@+id/button4"
android:onClick="postRequestParam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆" />
</LinearLayout>
3. loginActivity。【重要!! 这部分包含了get、post】
package com.example.mainapp_bleda2;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.mainapp_bleda2.bean.PostRequest;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class LogiActivity extends AppCompatActivity {
private static final String TAG = "Login";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login2);
}
public void getRequest(View view) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(8000, TimeUnit.MICROSECONDS).build();
Request request = new Request.Builder()
.get()
.url("http://172.20.10.2:8080/user/selectAll")
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NonNull okhttp3.Call call, @NonNull Response response) throws IOException {
Log.e(TAG, "onResponse: " + response.code());
Log.e(TAG, "onResponse: " + response.body().string());
}
@Override
public void onFailure(@NonNull okhttp3.Call call, @NonNull IOException e) {
Log.e(TAG, "onFailure" + e.toString());
}
});
}
public void getRequestParam(View view) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(8000, TimeUnit.MICROSECONDS).build();
Map<String, String> map = new HashMap<>();
map.put("user_account", "444");
map.put("user_pwd", "111");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("?");
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()){
Map.Entry<String, String> next = iterator.next();
stringBuilder.append(next.getKey());
stringBuilder.append("=");
stringBuilder.append(next.getValue());
if (iterator.hasNext()){
stringBuilder.append("&");
}
}
String str = stringBuilder.toString();
Request request = new Request.Builder()
.get()
.url("http://172.20.10.2:8080/user/register"+str)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(@NonNull okhttp3.Call call, @NonNull Response response) throws IOException {
Log.e(TAG, "onResponse: " + response.code());
Log.e(TAG, "onResponse: " + response.body().string());
}
@Override
public void onFailure(@NonNull okhttp3.Call call, @NonNull IOException e) {
Log.e(TAG, "onFailure" + e.toString());
}
});
}
public void postRequestParam(View view) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(8000, TimeUnit.MICROSECONDS).build();
PostRequest request = new PostRequest();
request.setUser_account("111");
request.setUser_pwd("1111");
Gson gson = new Gson();
String jsonStr = gson.toJson(request);
MediaType mediaType = MediaType.parse("application/json");
RequestBody requestBody = RequestBody.create(jsonStr, mediaType);
Request request1 = new Request.Builder()
.post(requestBody)
.url("http://172.20.10.2:8080/user/login")
.build();
okHttpClient.newCall(request1).enqueue(new Callback() {
@Override
public void onResponse(@NonNull okhttp3.Call call, @NonNull Response response) throws IOException {
System.out.println("111");
Log.e(TAG, "onResponse: " + response.code());
Log.e(TAG, "onResponse: " + response.body().string());
}
@Override
public void onFailure(@NonNull okhttp3.Call call, @NonNull IOException e) {
System.out.println("222");
Log.e(TAG, "onFailure" + e.toString());
}
});
}
}
4. postRequest.java. [这部分是post带参数需要的Bean 实体类]
package com.example.mainapp_bleda2.bean;
public class PostRequest {
private String user_account;
private String user_pwd;
public String getUser_account() {
return user_account;
}
public void setUser_account(String user_account) {
this.user_account = user_account;
}
public String getUser_pwd() {
return user_pwd;
}
public void setUser_pwd(String user_pwd) {
this.user_pwd = user_pwd;
}
}
学习于这个视频:戳-哔哩哔哩上的一个视频
|