Android开发学习笔记整理(16)-网络请求

代码部分:

part1:

logo.png

在这里插入图片描述

mobile.png

请添加图片描述

secret.png

请添加图片描述

btn_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#32C999"/>
    <corners android:radius="30dp"/>

</shape>
效果图:

在这里插入图片描述

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            app:srcCompat="@drawable/mobile" />

        <EditText
            android:id="@+id/et_mobile"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:ems="10"
            android:hint="请输入手机号"
            android:inputType="phone" />

    </LinearLayout>

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#DFDDDD"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout"
        app:layout_constraintStart_toStartOf="@+id/linearLayout"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/view">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            app:srcCompat="@drawable/secret" />

        <EditText
            android:id="@+id/et_password"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:ems="10"
            android:hint="请输入密码"
            android:inputType="textPassword" />
    </LinearLayout>

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#DFDDDD"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout2"
        app:layout_constraintStart_toStartOf="@+id/linearLayout2"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout2" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="40dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/btn_shape"
        android:onClick="login"
        android:text="登录"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@null"
        android:text="忘记密码"
        app:layout_constraintStart_toStartOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@null"
        android:onClick="register"
        android:text="注册"
        app:layout_constraintEnd_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>
效果图:

在这里插入图片描述

activity_register.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".RegisterActivity">

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView4">

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            app:srcCompat="@drawable/mobile" />

        <EditText
            android:id="@+id/et_mobile"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:ems="10"
            android:hint="请输入手机号"
            android:inputType="phone" />
    </LinearLayout>

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#DFdddd"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout3"
        app:layout_constraintStart_toStartOf="@+id/linearLayout3"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3" />

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout3"
        app:layout_constraintStart_toStartOf="@+id/linearLayout3"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout3">

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            app:srcCompat="@drawable/secret" />

        <EditText
            android:id="@+id/et_password"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:ems="10"
            android:hint="请输入密码"
            android:inputType="textPassword" />
    </LinearLayout>

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#DFDDDD"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout4"
        app:layout_constraintStart_toStartOf="@+id/linearLayout4"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout4" />

    <LinearLayout
        android:id="@+id/linearLayout5"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout4"
        app:layout_constraintStart_toStartOf="@+id/linearLayout4"
        app:layout_constraintTop_toBottomOf="@+id/view4">

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            app:srcCompat="@drawable/secret" />

        <EditText
            android:id="@+id/et_repassword"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@null"
            android:ems="10"
            android:hint="请确认密码"
            android:inputType="textPassword" />
    </LinearLayout>

    <View
        android:id="@+id/view5"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#DDDDDD"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout5"
        app:layout_constraintStart_toStartOf="@+id/linearLayout5"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout5" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/btn_shape"
        android:onClick="register"
        android:text="注册"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout5"
        app:layout_constraintStart_toStartOf="@+id/linearLayout5"
        app:layout_constraintTop_toBottomOf="@+id/view5" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="@+id/button4"
        app:layout_constraintStart_toStartOf="@+id/button4"
        app:layout_constraintTop_toBottomOf="@+id/button4">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="已有账号,"
            android:textColor="#111111" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:text="立即登录"
            android:textColor="#32C999" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
效果图:

在这里插入图片描述

RegisterActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class RegisterActivity extends AppCompatActivity {

    private EditText et_mobile;
    private EditText et_password;
    private EditText et_repassword;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            // 提示注册成功了
            if ("200".equals(msg.obj)) {
                Toast.makeText(RegisterActivity.this,"注册成功", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(RegisterActivity.this, MainActivity.class));
            } else {
                Toast.makeText(RegisterActivity.this,"注册失败", Toast.LENGTH_SHORT).show();
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        et_mobile = findViewById(R.id.et_mobile);
        et_password = findViewById(R.id.et_password);
        et_repassword = findViewById(R.id.et_repassword);

    }

    /**
     * 注册
     * @param view
     */
    public void register(View view) {
        // 判断手机号是否为空
        final String mobile = et_mobile.getText().toString().trim();
        if ("".equals(mobile)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        // 判断密码是否为空
        final String password = et_password.getText().toString().trim();
        if ("".equals(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        // 判断两次输入的密码
        String repassword = et_repassword.getText().toString().trim();
        if (!password.equals(repassword)) {
            Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        // 发起http请求,进行注册,凡是费时的操作,要放到线程中去做
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL("http://121.36.161.94:8080/register");
                    connection = (HttpURLConnection) url.openConnection();
                    // 设置请求方式
                    connection.setRequestMethod("POST");
                    String param = "username="+mobile+"&password="+password;

                    // 获取输出流并输出参数
                    OutputStream outputStream = connection.getOutputStream();
                    outputStream.write(param.getBytes());
                    outputStream.close();

                    // 接收返回值
                    InputStream inputStream = connection.getInputStream();
                    int i = inputStream.read();
                    StringBuffer response = new StringBuffer();
                    while(i != -1) {
                        response.append((char)i);
                        i = inputStream.read();
                    }
                    inputStream.close();

                    // 输出结果
                    Log.i("RegisterActivity", response.toString());
                    // {"code":"200","msg":"success","data":null}称为json数据
                    JSONObject jsonObject = new JSONObject(response.toString());
                    String string = jsonObject.getString("code");
                    Message message = new Message();
                    message.obj = string;
                    handler.sendMessage(message);

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

}

IndexActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".IndexActivity">

</androidx.constraintlayout.widget.ConstraintLayout>

IndexActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class IndexActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.day15">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
        <activity android:name=".IndexActivity"></activity>
        <activity android:name=".RegisterActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {

    private EditText et_mobile;
    private EditText et_password;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            // 处理登录成功后
            if ("200".equals(msg.obj)) {
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(MainActivity.this, IndexActivity.class));
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_mobile = findViewById(R.id.et_mobile);
        et_password = findViewById(R.id.et_password);
    }

    /**
     * 注册
     * @param view
     */
    public void register(View view) {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }

    /**
     * 登录
     * @param view
     */
    public void login(View view) {
        final String username = et_mobile.getText().toString().trim();
        if ("".equals(username)) {
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        final String password = et_password.getText().toString().trim();
        if ("".equals(password)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
        }

        // 发送请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL("http://121.36.161.94:8080/login");
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");

                    // 向服务器传递参数
                    String param = "username="+username+"&password="+password;
                    OutputStream out = connection.getOutputStream();
                    out.write(param.getBytes());
                    out.close();

                    // 获取从服务器返回的数据
                    InputStream in = connection.getInputStream();
                    StringBuffer response = new StringBuffer();
                    int i = in.read();
                    while (i != -1) {
                        response.append((char)i);
                        i = in.read();
                    }
                    in.close();

                    // 提示登陆成功,并跳转主页
                    JSONObject jsonObject = new JSONObject(response.toString());
                    Message message = new Message();
                    message.obj = jsonObject.getString("code");
                    handler.sendMessage(message);

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }).start();
    }

}

part2:

rignt.png

在这里插入图片描述

add.png

请添加图片描述

book.png

请添加图片描述

back.png

请添加图片描述

btn_edit_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#32C999"/>
    <corners android:radius="2dp"/>

</shape>
效果图:

在这里插入图片描述

btn_delete_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="#E8BD48"/>
    <corners android:radius="2dp"/>

</shape>
效果图:

在这里插入图片描述

activity_index.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".IndexActivity">

    <LinearLayout
        android:id="@+id/linearLayout6"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:onClick="bookList"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center_vertical"
            app:srcCompat="@drawable/book" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:text="书籍管理"
            android:textColor="#111111"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/imageView9"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="center_vertical"
            app:srcCompat="@drawable/right" />
    </LinearLayout>

    <View
        android:id="@+id/view6"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#DDDDDD"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout6" />

</androidx.constraintlayout.widget.ConstraintLayout>
效果图:

在这里插入图片描述

title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#32C999"
    >

    <ImageButton
        android:id="@+id/btn_back"
        android:layout_width="24dp"
        android:layout_height="24dp"
        app:srcCompat="@drawable/back"
        android:background="@null"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"/>

   <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="书籍列表"
        android:layout_centerInParent="true"
        android:textColor="#111111"
        android:textSize="20sp"
        />

    <ImageButton
        android:id="@+id/btn_edit"
        android:layout_width="24dp"
        android:layout_height="24dp"
        app:srcCompat="@drawable/add"
        android:layout_alignParentRight="true"
        android:background="@null"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dp"/>

</RelativeLayout>
效果图:

在这里插入图片描述

activity_book_list.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".BookListActivity">

    <include
        android:id="@+id/include"
        layout="@layout/title" />

    <ListView
        android:id="@+id/bookListView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/include" />

</androidx.constraintlayout.widget.ConstraintLayout>
效果图:

在这里插入图片描述

activity_book_add.xml

<?xml version="1.0" encoding="utf-8"?>
<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=".BookAddActivity">

    <include
        android:id="@+id/include2"
        layout="@layout/title" />

    <LinearLayout
        android:id="@+id/linearLayout7"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/include2">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="20dp"
            android:text="书籍名称"
            android:textColor="#111111"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:background="@null"
            android:ems="10"
            android:gravity="right|center_vertical"
            android:hint="请输入书籍名称"
            android:inputType="textPersonName" />
    </LinearLayout>

    <View
        android:id="@+id/view7"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#dddddd"
        app:layout_constraintEnd_toEndOf="@+id/linearLayout7"
        app:layout_constraintStart_toStartOf="@+id/linearLayout7"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout7" />

    <LinearLayout
        android:id="@+id/linearLayout8"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view7">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="出版时间"
            android:textColor="#111111"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_publishing"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:background="@null"
            android:ems="10"
            android:gravity="right|center_vertical"
            android:hint="请输入出版时间"
            android:inputType="textPersonName" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout9"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view8">

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="书籍简介"
            android:textColor="#111111"
            android:textSize="16sp" />

        <EditText
            android:id="@+id/et_intro"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="20dp"
            android:layout_weight="1"
            android:background="@null"
            android:ems="10"
            android:gravity="right|center_vertical"
            android:hint="请输入书籍简介"
            android:inputType="textPersonName" />
    </LinearLayout>

    <View
        android:id="@+id/view8"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#DDDDDD"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout8" />

    <View
        android:id="@+id/view9"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:background="#DDDDDD"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout9" />

    <Button
        android:id="@+id/button6"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginStart="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="80dp"
        android:layout_marginEnd="20dp"
        android:layout_marginRight="20dp"
        android:background="@drawable/btn_shape"
        android:onClick="addBook"
        android:text="保存"
        android:textColor="#FFFFFF"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view9" />

</androidx.constraintlayout.widget.ConstraintLayout>
效果图:

在这里插入图片描述

BookAddActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONObject;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class BookAddActivity extends AppCompatActivity {

    private EditText et_name;
    private EditText et_publishing;
    private EditText et_intro;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            if("200".equals(msg.obj)) {
                // 提示保存成功
                Toast.makeText(BookAddActivity.this, "书籍添加成功", Toast.LENGTH_SHORT).show();
                // 发送广播
                LocalBroadcastManager.getInstance(BookAddActivity.this).sendBroadcast(new Intent("day15.book.add"));
                // 关闭本界面
                finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_add);

        et_name = findViewById(R.id.et_name);
        et_publishing = findViewById(R.id.et_publishing);
        et_intro = findViewById(R.id.et_intro);

        // 隐藏右边按钮
        findViewById(R.id.btn_edit).setVisibility(View.INVISIBLE);

        // 修改标题
        TextView textView = findViewById(R.id.tv_title);
        textView.setText("书籍添加");

        // 返回按钮
        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 保存书籍
     * @param view
     */
    public void addBook(View view) {

        final String name = et_name.getText().toString().trim();
        final String publishing = et_publishing.getText().toString().trim();
        final String intro = et_intro.getText().toString().trim();

        // 发请求
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL("http://121.36.161.94:8080/book/addBook");
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");

                    // 准备参数并发送
                    String param = "name="+name+"&publishing="+publishing+"&intro="+intro;
                    OutputStream out = connection.getOutputStream();
                    out.write(param.getBytes());
                    out.close();

                    // 处理返回结果
                    InputStream in = connection.getInputStream();
                    StringBuffer response = new StringBuffer();
                    int i = in.read();
                    while(i != -1) {
                        response.append((char)i);
                        i = in.read();
                    }

                    JSONObject jsonObject = new JSONObject(response.toString());
                    Message message = new Message();
                    message.obj = jsonObject.getString("code");
                    handler.sendMessage(message);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}

BookListActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class BookListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_list);

        // 跳转添加界面
        findViewById(R.id.btn_edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BookListActivity.this, BookAddActivity.class));
            }
        });

        // 返回事件
        findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.day15">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"
        android:usesCleartextTraffic="true">
        <activity android:name=".BookAddActivity"></activity>
        <activity android:name=".BookListActivity" />
        <activity android:name=".IndexActivity" />
        <activity android:name=".RegisterActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值