首页 > 其他分享 >3.11

3.11

时间:2024-03-11 21:14:45浏览次数:14  
标签:Toast 3.11 安卓 import new android void

今天实现通过安卓连接web后端 最后在mysql数据库添加数据库的操作

在安卓项目中首先在AndroidMainfest.xml中添加链接网络权限,同时允许安卓明文传输所要连接的IP地址

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
    <application
            android:allowBackup="true"
            android:dataExtractionRules="@xml/data_extraction_rules"
            android:fullBackupContent="@xml/backup_rules"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.MySqlWeb"
            tools:targetApi="31"
            android:networkSecurityConfig="@xml/network_security_config">
        <activity
                android:name=".MainActivity"
                android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

</manifest>

然后在xml配置文件中添加上文所提到到的允许明文传输的文件

 上述图片中xml文件夹的最后一个文件

<?xml version="1.0" encoding="utf-8"?>
<!-- 解决 Android P 禁用 http 请求的问题 -->
<network-security-config>
    <!-- 允许明文通信 -->
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

然后在布局中添加 用户名 年龄的文本框和按钮

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#00f"
                android:text="用户名"/>
        <EditText
                android:id="@+id/name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#00f"
                android:hint="请输入用户名"/>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:textColor="#00f"
                android:text="年 龄"/>
        <EditText
                android:id="@+id/age"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:inputType="number"
                android:textColor="#00f"
                android:hint="请输入年龄"/>
    </LinearLayout>
    <Button
            android:text="添加"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:id="@+id/button"/>
</androidx.appcompat.widget.LinearLayoutCompat>

然后再MainActivity文件中连接后端 同时接受文本框的值 为按钮定义点击事件 传递给后端

package com.zhen.mysqlweb;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import okhttp3.*;

public class MainActivity extends AppCompatActivity {
private EditText nameEdit;
private EditText ageEdit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.button);
        nameEdit = findViewById(R.id.name);
        ageEdit=findViewById(R.id.age);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String name=nameEdit.getText().toString();
                            int age=Integer.parseInt(ageEdit.getText().toString());
                            String json = "{\n" +
                                    "\t\"name\": \""+name+"\",\n" +
                                    " \t\"age\":" +age+"\n" +
                                    "}";
                            OkHttpClient client = new OkHttpClient();//创建http客户端
                            Request request = new Request.Builder()
                                    .url("http://10.99.113.50:8080/user/add")
                                    .post(RequestBody.create(MediaType.parse("application/json"), json))
                                    .build();//创造http请求
                            //回复
                            Response response = client.newCall(request).execute();//execute表示执行
                            runOnUiThread(() -> Toast.makeText(MainActivity.this, "请求成功", Toast.LENGTH_SHORT).show());

                        } catch (Exception e) {
                            e.printStackTrace();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }
                }).start();
            }
        });
    }
}

要注意这里的10.99.113.50是我本地电脑的ip地址,你需要替换成自己的,需要在控制台中输入ipconfig查看自己本地电脑的ip地址,这也就意味着你再运行项目时是需要联网的。这样安卓端的代码就实现了,然后通过springboot后端写一个接口传递数据,通过controller service mapper三层架构处理数据传递给mysql数据库处理请求,最终实现数据库数据的增。

标签:Toast,3.11,安卓,import,new,android,void
From: https://www.cnblogs.com/zzqq1314/p/18067034

相关文章

  • 软件工程日报5 2024.03.11
     第一天第二天第三天第四天第五天所花时间(包括上课)6小时5小时4小时4小时 六小时代码量(行)300350200300 50博客量(篇)1111 1所学知识了解安卓相关数据库的知识,下载安装了matlab学习了相关安卓的布局展示了解activity之间的相互跳转以注册了......
  • 3.8~3.11闲话
    3.8因为教师资格证考试所以放假......
  • 鲜花 3.11
    这是一篇鲜花。我认为鲜花是相当好的,因为可以乐乐乐。最近精神状态一直不大好,连续n天没有写题了,鉴定为不打隔膜导致的/cf/cf/cf上周把WA2coda推完了,可能还有一个后日谈,这周回去推。怎么还有巨大的任务计划,破防了。看到hanghang的鲜花,感觉神秘敬酒环节有点可怕的,还好我......
  • anaconda环境下:强化学习PPO算法仿真环境库sample-factory的python完美适配版本为pytho
    anaconda环境下:强化学习PPO算法仿真环境库sample-factory的python完美适配版本为python3.11库sample-factory地址:https://github.com/alex-petrenko/sample-factory文档地址:https://samplefactory.dev/经过对多个版本的python进行测试,anaconda环境下只有python3.11......
  • docker安装rabbitmq3.11.9镜像集群
    准备3台服务器安装配置镜像集群IP1:192.168.0.1IP2:192.168.0.2IP3:192.168.0.3yuminstall-ylrzsztelnetnciftopunzipyum-utilsepel-releasesystemctlstopfirewalld.service&&systemctldisablefirewalld.serviceyum-config-manager--add-repohttp://mirrors.aliyu......
  • KubeSphere 社区双周报 | 苏州 Meetup 报名开启 | 2023.11.23-12.07
    KubeSphere社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过commit的贡献者,并对近期重要的PR进行解析,同时还包含了线上/线下活动和布道推广等一系列社区动态。本次双周报涵盖时间为:2023.11.23-2023.12.07。贡献者名单新晋KubeSphereTale......
  • 2023衡山论坛 医学大数据与人工智能前沿论坛 2023.11.20-12
    中国抗癌协会肿瘤标志专业委员会南华大学附属第一医院南华大学计算机学院 湖南省生物医学工程协会 湖南省生物信息学会哈尔滨工业大学生命科学与技术学院   湖南省生物医学工程学会是由湖南省从事生物医学工程学科活动的科技工作者和有志于促进生物医学工程领域......
  • #6 2023.11.26
    395.arc140dOnetoOne原图是基环树森林。先把已知的边连上,会变成若干个基环树和若干个内向树。我们对环的期望数量计数。对于基环树,贡献显然为1。对于内向树,我们枚举大小的有序序列\(v_1,..,v_k\),贡献是\({1\overk}\prod{v_i\overn}\)。分治NTT算即可。396.a......
  • 2023.11.30 练习
    CF1887C首先容易想到区间加需转化为差分,字典序的比较呢就考虑二分哈希。二分第一个不一样的位置,这个位置也一定是差分数组第一个不一样的。把哈希如果放到线段树上,那么在线段树上二分即可。我们依次处理修改的时候,顺便处理当前的最小的字典序。我们这里如果采用主席树,那么会......
  • 2023.11.29——每日总结
    学习所花时间(包括上课):9h代码量(行):0行博客量(篇):1篇今天,上午学习,下午学习;我了解到的知识点:1.百度图像增强SDK明日计划:学习......