mysql数据库表里的情况:
安卓可以‘user_table’中的数据,注册新增用户,用户登陆。【写的很简单】
implementation 'com.squareup.okhttp3:okhttp:4.4.1'
implementation 'com.google.code.gson:gson:2.8.5'
<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>
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);
}
//get请求不带参数
public void getRequest(View view) {
//先来okhttp客户端
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());
}
});
}
//get请求带参数
public void getRequestParam(View view) {
//先来okhttp客户端
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());
}
});
}
//post请求带参数
public void postRequestParam(View view) {
//先来okhttp客户端
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());
}
});
}
}
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;
}
}
学习于这个视频:戳-哔哩哔哩上的一个视频