ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Android】as通过okhttp3实现对图书管理系统的增删改查

2021-11-25 21:30:47  阅读:294  来源: 互联网

标签:String 改查 okhttp3 json result new Android id name


Android Studio通过SpringBoot连接MySQL数据库实现增删查改

文章目录

前言

Android通过jdbc连接MySQL只可以连接MySQL5.1.x,不可以连接MySQL8.0.x
也不推荐使用Android应用程序中的JDBC,它是不安全的,并且通常无法直接连接到数据库。

springboot连接mysql

后端可以参考
SpringBoot连接MySQL获取数据写后端接口

Android Studio通过okhttp3网络请求实现增删查改

在这里插入图片描述

引入依赖

implementation "com.squareup.okhttp3:okhttp:4.9.2"

activity_main

在这里插入图片描述

<?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=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="as访问MySQL测试"
        android:textColor="#03A9F4"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn_count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查询" />

    <TextView
        android:id="@+id/tv_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="id"
        android:textColor="#F44336"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="name"
        android:textColor="#F44336"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="number"
        android:textColor="#F44336"
        android:textSize="20sp"
        android:textStyle="bold" />
    <TextView
        android:id="@+id/tv_author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="author"
        android:textColor="#F44336"
        android:textSize="20sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btn_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="添加" />

    <EditText
        android:id="@+id/et_add_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="name"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_add_author"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="author"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_add_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="number"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/btn_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="删除" />

    <EditText
        android:id="@+id/et_delete_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="name"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/btn_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="修改" />

    <EditText
        android:id="@+id/et_update_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="id"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_update_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="name"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_update_author"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="author"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/et_update_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="number"
        android:inputType="textPersonName" />

</LinearLayout>

MainActivity

在这里插入图片描述

onCreate

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_count = findViewById(R.id.btn_count);
        btn_add = findViewById(R.id.btn_add);
        btn_delete = findViewById(R.id.btn_delete);
        btn_update = findViewById(R.id.btn_update);
        tv_name = findViewById(R.id.tv_name);
        tv_id = findViewById(R.id.tv_id);
        tv_number = findViewById(R.id.tv_number);
        tv_author = findViewById(R.id.tv_author);
        et_add_name = findViewById(R.id.et_add_name);
        et_add_author = findViewById(R.id.et_add_author);
        et_add_number = findViewById(R.id.et_add_number);
        et_delete_name = findViewById(R.id.et_delete_name);
        et_update_id = findViewById(R.id.et_update_id);
        et_update_name = findViewById( R.id.et_update_name);
        et_update_author = findViewById(R.id.et_update_author);
        et_update_number = findViewById(R.id.et_update_number);

        btn_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getBookList();
            }
        });
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addBook(et_add_name.getText().toString(),et_add_author.getText().toString(),et_add_number.getText().toString());
            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteBook(et_delete_name.getText().toString());
            }
        });
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateBook(et_update_id.getText().toString(),et_update_name.getText().toString(),et_update_author.getText().toString(),et_update_number.getText().toString());
            }
        });
    }

查询数据

 public void getBookList(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String name = null,id = null,number = null,author = null;
                    OkHttpClient client =new OkHttpClient();
                    FormBody.Builder requestBuild=new FormBody.Builder();
                    Request request=new Request.Builder()
                            .url("http://10.138.121.198:8080/springboot/getbookList") // url
                            .build();
                    Call call = client.newCall(request);
                    Response response = call.execute();
                    String  result = response.body().string();
                    Log.i("test", result);
                    JSONArray result_json=new JSONArray(result);
                    for(int i=0;i<result_json.length();i++) {
                        id=result_json.getJSONObject(i).getString("id");
                        name=result_json.getJSONObject(i).getString("name");
                        number=result_json.getJSONObject(i).getString("number");
                        author=result_json.getJSONObject(i).getString("author");
                    }
                    Handler mainHandler = new Handler(Looper.getMainLooper());
                    String finalName = name;
                    String finalId = id;
                    String finalNumber = number;
                    String finalAuthor = author;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            //已在主线程中,可以更新UI
                            tv_name.setText(finalName);
                            tv_author.setText(finalAuthor);
                            tv_number.setText(finalNumber);
                            tv_id.setText(finalId);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

添加数据

public void addBook(String name,String author,String number){

        Thread thread =new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json =new JSONObject();
                    json.put("addName" , name);
                    json.put("addAuthor" , author);
                    json.put("addNumber" , number);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));

                    Log.i("test",name+" "+author+" "+number);
                    Request request = new Request.Builder()
                            .url("http://10.138.121.198:8080/springboot/addBook") // url
                            .post(requestBody)
                            .header("contentType","application/json;charset=utf-8")

                            .build();
                    Call call = client.newCall(request);
                    Response response = call.execute();

                    String  result = response.body().string();
                    Log.i("test", result);
                    JSONArray result_json=new JSONArray(result);
                    String getName=null,getId=null,getNumber=null,getAuthor=null;
                    for(int i=0;i<result_json.length();i++) {
                        getName=result_json.getJSONObject(i).getString("id");
                        getId=result_json.getJSONObject(i).getString("name");
                        getNumber=result_json.getJSONObject(i).getString("number");
                        getAuthor=result_json.getJSONObject(i).getString("author");
                    }
                    Handler mainHandler = new Handler(Looper.getMainLooper());
                    String finalName = getName;
                    String finalId = getId;
                    String finalNumber = getNumber;
                    String finalAuthor = getAuthor;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            //已在主线程中,可以更新UI
                            tv_name.setText(finalName);
                            tv_author.setText(finalAuthor);
                            tv_number.setText(finalNumber);
                            tv_id.setText(finalId);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();

    }

删除数据

 public void deleteBook(String name){
        Thread thread =new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json =new JSONObject();
                    json.put("deleteName", name);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
                    Request request = new Request.Builder()
                            .url("http://10.138.121.198:8080/springboot/deleteBook") // url
                            .post(requestBody)
                            .build();
                    Call call = client.newCall(request);
                    Response response = call.execute();
                    String  result = response.body().string();
                    Log.i("test", result);
                    JSONArray result_json=new JSONArray(result);
                    String getName=null,getId=null,getNumber=null,getAuthor=null;
                    for(int i=0;i<result_json.length();i++) {
                        getName=result_json.getJSONObject(i).getString("id");
                        getId=result_json.getJSONObject(i).getString("name");
                        getNumber=result_json.getJSONObject(i).getString("number");
                        getAuthor=result_json.getJSONObject(i).getString("author");
                    }
                    Handler mainHandler = new Handler(Looper.getMainLooper());
                    String finalName = getName;
                    String finalId = getId;
                    String finalNumber = getNumber;
                    String finalAuthor = getAuthor;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            //已在主线程中,可以更新UI
                            tv_name.setText(finalName);
                            tv_author.setText(finalAuthor);
                            tv_number.setText(finalNumber);
                            tv_id.setText(finalId);
                        }
                    });
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();

    }

修改数据

 public void updateBook(String id ,String name,String author,String number){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json =new JSONObject();
                    json.put("updateName" , name);
                    json.put("updateAuthor" , author);
                    json.put("updateNumber" , number);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
                    Request request = new Request.Builder()
                            .url("http://10.138.121.198:8080/springboot/updateBook") // url
                            .post(requestBody)
                            .build();
                    Call call = client.newCall(request);
                    Response response = call.execute();
                    String  result = response.body().string();
                    Log.i("test", result);
                    JSONArray result_json=new JSONArray(result);
                    String getName=null,getId=null,getNumber=null,getAuthor=null;
                    for(int i=0;i<result_json.length();i++) {
                        getName=result_json.getJSONObject(i).getString("id");
                        getId=result_json.getJSONObject(i).getString("name");
                        getNumber=result_json.getJSONObject(i).getString("number");
                        getAuthor=result_json.getJSONObject(i).getString("author");
                    }
                    Handler mainHandler = new Handler(Looper.getMainLooper());
                    String finalName = getName;
                    String finalId = getId;
                    String finalNumber = getNumber;
                    String finalAuthor = getAuthor;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            //已在主线程中,可以更新UI
                            tv_name.setText(finalName);
                            tv_author.setText(finalAuthor);
                            tv_number.setText(finalNumber);
                            tv_id.setText(finalId);
                        }
                    });
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }

完整代码


import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class MainActivity extends AppCompatActivity {
    Button btn_count;
    Button btn_add;
    Button btn_delete;
    Button btn_update;
    TextView tv_name;
    TextView tv_id;
    TextView tv_number;
    TextView tv_author;
    EditText et_add_name;
    EditText et_add_author;
    EditText et_add_number;
    EditText et_delete_name;
    EditText et_update_id;
    EditText et_update_name;
    EditText et_update_author;
    EditText et_update_number;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_count = findViewById(R.id.btn_count);
        btn_add = findViewById(R.id.btn_add);
        btn_delete = findViewById(R.id.btn_delete);
        btn_update = findViewById(R.id.btn_update);
        tv_name = findViewById(R.id.tv_name);
        tv_id = findViewById(R.id.tv_id);
        tv_number = findViewById(R.id.tv_number);
        tv_author = findViewById(R.id.tv_author);
        et_add_name = findViewById(R.id.et_add_name);
        et_add_author = findViewById(R.id.et_add_author);
        et_add_number = findViewById(R.id.et_add_number);
        et_delete_name = findViewById(R.id.et_delete_name);
        et_update_id = findViewById(R.id.et_update_id);
        et_update_name = findViewById( R.id.et_update_name);
        et_update_author = findViewById(R.id.et_update_author);
        et_update_number = findViewById(R.id.et_update_number);

        btn_count.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getBookList();
            }
        });
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addBook(et_add_name.getText().toString(),et_add_author.getText().toString(),et_add_number.getText().toString());
            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteBook(et_delete_name.getText().toString());
            }
        });
        btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateBook(et_update_id.getText().toString(),et_update_name.getText().toString(),et_update_author.getText().toString(),et_update_number.getText().toString());
            }
        });
    }
    //查询数据
    public void getBookList(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String name = null,id = null,number = null,author = null;
                    OkHttpClient client =new OkHttpClient();
                    FormBody.Builder requestBuild=new FormBody.Builder();
                    Request request=new Request.Builder()
                            .url("http://10.138.121.198:8080/springboot/getbookList") // url
                            .build();
                    Call call = client.newCall(request);
                    Response response = call.execute();
                    String  result = response.body().string();
                    Log.i("test", result);
                    JSONArray result_json=new JSONArray(result);
                    for(int i=0;i<result_json.length();i++) {
                        id=result_json.getJSONObject(i).getString("id");
                        name=result_json.getJSONObject(i).getString("name");
                        number=result_json.getJSONObject(i).getString("number");
                        author=result_json.getJSONObject(i).getString("author");
                    }
                    Handler mainHandler = new Handler(Looper.getMainLooper());
                    String finalName = name;
                    String finalId = id;
                    String finalNumber = number;
                    String finalAuthor = author;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            //已在主线程中,可以更新UI
                            tv_name.setText(finalName);
                            tv_author.setText(finalAuthor);
                            tv_number.setText(finalNumber);
                            tv_id.setText(finalId);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //添加数据
    public void addBook(String name,String author,String number){

        Thread thread =new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json =new JSONObject();
                    json.put("addName" , name);
                    json.put("addAuthor" , author);
                    json.put("addNumber" , number);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));

                    Log.i("test",name+" "+author+" "+number);
                    Request request = new Request.Builder()
                            .url("http://10.138.121.198:8080/springboot/addBook") // url
                            .post(requestBody)
                            .header("contentType","application/json;charset=utf-8")

                            .build();
                    Call call = client.newCall(request);
                    Response response = call.execute();

                    String  result = response.body().string();
                    Log.i("test", result);
                    JSONArray result_json=new JSONArray(result);
                    String getName=null,getId=null,getNumber=null,getAuthor=null;
                    for(int i=0;i<result_json.length();i++) {
                        getName=result_json.getJSONObject(i).getString("id");
                        getId=result_json.getJSONObject(i).getString("name");
                        getNumber=result_json.getJSONObject(i).getString("number");
                        getAuthor=result_json.getJSONObject(i).getString("author");
                    }
                    Handler mainHandler = new Handler(Looper.getMainLooper());
                    String finalName = getName;
                    String finalId = getId;
                    String finalNumber = getNumber;
                    String finalAuthor = getAuthor;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            //已在主线程中,可以更新UI
                            tv_name.setText(finalName);
                            tv_author.setText(finalAuthor);
                            tv_number.setText(finalNumber);
                            tv_id.setText(finalId);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();

    }
    //删除数据
    public void deleteBook(String name){
        Thread thread =new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json =new JSONObject();
                    json.put("deleteName", name);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
                    Request request = new Request.Builder()
                            .url("http://10.138.121.198:8080/springboot/deleteBook") // url
                            .post(requestBody)
                            .build();
                    Call call = client.newCall(request);
                    Response response = call.execute();
                    String  result = response.body().string();
                    Log.i("test", result);
                    JSONArray result_json=new JSONArray(result);
                    String getName=null,getId=null,getNumber=null,getAuthor=null;
                    for(int i=0;i<result_json.length();i++) {
                        getName=result_json.getJSONObject(i).getString("id");
                        getId=result_json.getJSONObject(i).getString("name");
                        getNumber=result_json.getJSONObject(i).getString("number");
                        getAuthor=result_json.getJSONObject(i).getString("author");
                    }
                    Handler mainHandler = new Handler(Looper.getMainLooper());
                    String finalName = getName;
                    String finalId = getId;
                    String finalNumber = getNumber;
                    String finalAuthor = getAuthor;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            //已在主线程中,可以更新UI
                            tv_name.setText(finalName);
                            tv_author.setText(finalAuthor);
                            tv_number.setText(finalNumber);
                            tv_id.setText(finalId);
                        }
                    });
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();

    }
    //修改数据
    public void updateBook(String id ,String name,String author,String number){
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json =new JSONObject();
                    json.put("updateName" , name);
                    json.put("updateAuthor" , author);
                    json.put("updateNumber" , number);
                    RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8") ,String.valueOf(json));
                    Request request = new Request.Builder()
                            .url("http://10.138.121.198:8080/springboot/updateBook") // url
                            .post(requestBody)
                            .build();
                    Call call = client.newCall(request);
                    Response response = call.execute();
                    String  result = response.body().string();
                    Log.i("test", result);
                    JSONArray result_json=new JSONArray(result);
                    String getName=null,getId=null,getNumber=null,getAuthor=null;
                    for(int i=0;i<result_json.length();i++) {
                        getName=result_json.getJSONObject(i).getString("id");
                        getId=result_json.getJSONObject(i).getString("name");
                        getNumber=result_json.getJSONObject(i).getString("number");
                        getAuthor=result_json.getJSONObject(i).getString("author");
                    }
                    Handler mainHandler = new Handler(Looper.getMainLooper());
                    String finalName = getName;
                    String finalId = getId;
                    String finalNumber = getNumber;
                    String finalAuthor = getAuthor;
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            //已在主线程中,可以更新UI
                            tv_name.setText(finalName);
                            tv_author.setText(finalAuthor);
                            tv_number.setText(finalNumber);
                            tv_id.setText(finalId);
                        }
                    });
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }
}

最后

如果不懂可以私聊我哟。

标签:String,改查,okhttp3,json,result,new,Android,id,name
来源: https://blog.csdn.net/weixin_46946002/article/details/121506580

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有