Implementation ideas:
Android client input the account and password, use okHttp to send a request to the server and pass the input account and password, the login interface of the server queries the database, determines whether the login succeeds, and then returns the JSON string to the client, the client uses Gson to parse the data returned by the server and make different operations. Here is a very simple example.
Server:
1. The database
2. The web service
SpringBoot+ MyBtis-plus (SpringBoot+ MyBtis-Plus)
if(users.size() ! =0){
map.put("msg"."Login successful");
}else {
map.put("msg"."Incorrect account or password");
}
return map;
Copy the code
The point is at the Andre end
AnZhuoDuan:
1. Login page XML
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textUsername"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:text="Account:"
app:layout_constraintBottom_toBottomOf="@+id/editUsername"
app:layout_constraintEnd_toStartOf="@+id/editUsername"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/editUsername" />
<TextView
android:id="@+id/textPassword"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:text=Password:
app:layout_constraintBottom_toBottomOf="@+id/editPassword"
app:layout_constraintEnd_toStartOf="@+id/editPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/editPassword" />
<EditText
android:id="@+id/editUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
android:ems="10"
android:hint="Please enter user name/mobile number"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textUsername"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:layout_marginEnd="23dp"
android:layout_marginRight="23dp"
android:ems="10"
android:hint="Please enter your password"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textPassword"
app:layout_constraintTop_toBottomOf="@+id/editUsername" />
<Button
android:id="@+id/login"
android:layout_width="142dp"
android:layout_height="42dp"
android:layout_marginStart="200dp"
android:layout_marginLeft="200dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="57dp"
android:layout_marginRight="57dp"
android:text="Login"
app:layout_constraintEnd_toEndOf="@+id/editPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editPassword" />
</androidx.constraintlayout.widget.ConstraintLayout>
Copy the code
2. MainActivity.java
public class MainActivity extends AppCompatActivity {
private EditText editUsername;
private EditText editPassword;
// Server login interface
private String url = "http://192.168.17.xx:8081/user/login";
private String address = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve the data entered by the user
editUsername = findViewById(R.id.editUsername);
editPassword = findViewById(R.id.editPassword);
Button login = findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = editUsername.getText().toString();
String password = editPassword.getText().toString();
// Pass parameters to the server
address = url+"? username="+username+"&password="+password; login(); }}); }// Start a child thread in the login operation
private void login(a){
new Thread(new Runnable() {
@Override
public void run(a) {
try {
Response response = HttpUtil.sendOkHttpRequest(address);
if(response ! =null){
String responseData = response.body().string();
Map<String,Object> map = null;
// Use Gson to resolve the data returned by the server
Gson gson = new Gson();
Type type = new TypeToken<Map<String,Object>>(){}.getType();
map = gson.fromJson(responseData,type);
String msg = map.get("msg").toString();
// Determine whether the login succeeded or failed and pass data to the main thread
if("Login successful".equals(msg)){
Message message = new Message();
message.what = 1;
message.obj = msg;
handler.sendMessage(message);
}else{
Message message = new Message();
message.what = 2; message.obj = msg; handler.sendMessage(message); }}}catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
// Perform different operations depending on the data sent from the child thread
Handler handler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what){
case 1:
String data = (String) msg.obj;
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("msg",data);
startActivity(intent);
break;
case 2:
String data2 = (String) msg.obj;
Toast.makeText(MainActivity.this, data2, Toast.LENGTH_SHORT).show(); }}}; }Copy the code
The String url = “http://192.168.17.99:8081/user/login” is a server-side login interface,
The SecondActivity is to open a blank page, activity_second.xml, with no code attached.
HttpUtil is OkHttp.
public class HttpUtil {
public static Response sendOkHttpRequest(String address) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(address)
.build();
try {
Response response = client.newCall(request).execute();
return response;
} catch (IOException e) {
e.printStackTrace();
return null; }}}Copy the code
3. Run
Finally, run the server-side code, put the phone on the same network as the server, and you can test the login.
4. Pay attention to
Okhttp and gson dependencies should be added:
Implementation ‘com. Squareup. Okhttp3: okhttp: 4.2.2’ implementation ‘com. Google. Code. Gson: gson: 2.7’
Androidmanifest.xml open permission
Android: usesCleartextTraffic = “true” / / clear text transmission