冲刺成果演示

1.

 

 

 两个Bean文件:CostBean(用户)school(学校)

CostBean

package com.example.kaoyanpai;

public class school {
    private String name;
    private String fenshu;
    private String bili;
    private String jingyan;
    private String taolunzu;

    public school(String name, String fenshu, String bili, String jingyan, String taolunzu) {
        this.name = name;
        this.fenshu = fenshu;
        this.bili = bili;
        this.jingyan = jingyan;
        this.taolunzu = taolunzu;
    }

    public school() {
    }

    public String getName() {
        return name;
    }

    public String getFenshu() {
        return fenshu;
    }

    public String getBili() {
        return bili;
    }

    public String getJingyan() {
        return jingyan;
    }

    public String getTaolunzu() {
        return taolunzu;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setFenshu(String fenshu) {
        this.fenshu = fenshu;
    }

    public void setBili(String bili) {
        this.bili = bili;
    }

    public void setJingyan(String jingyan) {
        this.jingyan = jingyan;
    }

    public void setTaolunzu(String taolunzu) {
        this.taolunzu = taolunzu;
    }
}

  school

package com.example.kaoyanpai;

public class CostBean {
    private String name;
    private String password;

    public String getName() {
        return name;
    }

    public String getPassword() {
        return password;
    }

    public void setName(){

    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public  CostBean(){

    }

    public CostBean(String name, String password) {
        this.name = name;
        this.password = password;
    }
}

  db:连接数据库

package com.example.kaoyanpai;

import android.util.Log;

import com.mysql.jdbc.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class db {

    private static String driver = "com.mysql.jdbc.Driver";//MySQL 驱动
    private static String url = "jdbc:mysql://192.168.1.5:3306/yanhu?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull";
    private static String user = "root";//用户名
    private static String password = "zhangziyi1670";//密码
//192.168.1.5
    /**
     * 连接数据库
     * */

    public static Connection getConn(){
        Connection conn = null;
        try {
            Class.forName(driver);//获取MYSQL驱动
            conn = (Connection) DriverManager.getConnection(url, user, password);//获取连接
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 关闭数据库
     * */

    public static void closeAll(Connection conn, PreparedStatement ps){
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 关闭数据库
     * */

    public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs){
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
public static void main(String[] args){
    if(getConn()!=null){
        Log.i("message","ok");
    }
    else {
        Log.i("m","no");
    }
}
}

  DBService:

package com.example.kaoyanpai;

import android.util.Log;

import com.mysql.jdbc.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBService {
    private Connection conn = null; //打开数据库对象
    private PreparedStatement ps = null;//操作整合sql语句的对象
    private ResultSet rs = null;//查询结果的集合

    /**
     * 获取要发送短信的患者信息    查
     */

    public boolean  getUserData(String name){
        //结果存放集合
        List<CostBean> list=new ArrayList<CostBean>();
        boolean flag=false;
        //MySQL 语句
        String sql="select * from yanhu where name = '"+name+"'";
        //获取链接数据库对象
        conn= db.getConn();
        try {
            if(conn!=null&&(!conn.isClosed())){
                ps= (PreparedStatement) conn.prepareStatement(sql);
                if(ps!=null){
                    rs= ps.executeQuery();
                    if(rs!=null){
                        flag=true;
                    }
                    else flag=false;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        db.closeAll(conn,ps,rs);//关闭相关操作
        return flag;
    }
//查学校
public List<school>  searchschool(String name){
    //结果存放集合
    List<school> list=new ArrayList<school>();
    //MySQL 语句
    String sql="select * from school where name = '"+name+"'";
    //获取链接数据库对象
    conn= db.getConn();
    try {
        if(conn!=null&&(!conn.isClosed())){
            ps= (PreparedStatement) conn.prepareStatement(sql);
            if(ps!=null){
                rs= ps.executeQuery();
                if(rs.next()){
                    school school=new school();
                    school.setName(name);
                    school.setFenshu(rs.getString("fenshu"));
                    school.setBili(rs.getString("bili"));
                    school.setJingyan(rs.getString("jingyan"));
                    school.setTaolunzu(rs.getString("taolun"));
                    list.add(school);
                }
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    db.closeAll(conn,ps,rs);//关闭相关操作
    return list;
}
    /**
     * 修改数据库中某个对象的状态   改
     * */

    /*public int updateUserData(String phone){
        int result=-1;
        if(!StringUtils.isEmpty(phone)){
            //获取链接数据库对象
            conn= DBOpenHelper.getConn();
            //MySQL 语句
            String sql="update user set state=? where phone=?";
            try {
                boolean closed=conn.isClosed();
                if(conn!=null&&(!closed)){
                    ps= (PreparedStatement) conn.prepareStatement(sql);
                    ps.setString(1,"1");//第一个参数state 一定要和上面SQL语句字段顺序一致
                    ps.setString(2,phone);//第二个参数 phone 一定要和上面SQL语句字段顺序一致
                    result=ps.executeUpdate();//返回1 执行成功
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        DBOpenHelper.closeAll(conn,ps);//关闭相关操作
        return result;
    }

    /**
     * 批量向数据库插入数据   增
     * */
    public  int insertUserData(List<CostBean> list) {
        int result = -1;
        if ((list != null) && (list.size() > 0)) {
            //获取链接数据库对象
            conn = db.getConn();
            if(conn==null){
                Log.i("list","connnull");
            }
            //MySQL 语句
            String sql = "INSERT INTO yanhu (name,password) VALUES (?,?)";
            try {
                boolean closed = conn.isClosed();
                if ((conn != null) && (!closed)) {
                    for (CostBean user : list) {
                        ps = (PreparedStatement) conn.prepareStatement(sql);
                        String name = user.getName();
                        String password = user.getPassword();
                        ps.setString(1, name);//第一个参数 name 规则同上
                        ps.setString(2, password);//第二个参数 phone 规则同上

                        result = ps.executeUpdate();//返回1 执行成功
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        db.closeAll(conn, ps);//关闭相关操作
        return result;
    }



    /**
     * 删除数据  删
     * */

    /*public int delUserData(String phone) {
        int result = -1;
        if ((!StringUtils.isEmpty(phone)) && (PhoneNumberUtils.isMobileNumber(phone))) {
            //获取链接数据库对象
            conn = DBOpenHelper.getConn();
            //MySQL 语句
            String sql = "delete from user where phone=?";
            try {
                boolean closed = conn.isClosed();
                if ((conn != null) && (!closed)) {
                    ps = (PreparedStatement) conn.prepareStatement(sql);
                    ps.setString(1, phone);
                    result = ps.executeUpdate();//返回1 执行成功
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        DBOpenHelper.closeAll(conn, ps);//关闭相关操作
        return result;

    }*/
    public static void main(String[] args){
        List<CostBean>list=new ArrayList<CostBean>();
        CostBean costBean=new CostBean();
        costBean.setName("zhangziyi");
        costBean.setPassword("zhangziyi");
        list.add(costBean);
        //insertUserData(list);
    }
}

  

ScoViewParger:新建class
package com.example.kaoyanpai;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

public class ScoViewParger extends ViewPager {
    private Handler handler;
    public ScoViewParger(@NonNull Context context) {
        this(context,null);
    }

    public ScoViewParger(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        handler=new Handler(Looper.getMainLooper());



    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startLooper();
    }
    private void startLooper(){
        handler.postDelayed(mTask,4000);
    }
    private Runnable mTask=new Runnable() {
        @Override
        public void run() {
            int currentItem=getCurrentItem();
            currentItem++;
            setCurrentItem(currentItem);
            postDelayed(this,4000);
        }
    };
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        stopLooper();
    }
    private void stopLooper(){
        handler.removeCallbacks(mTask);
    }
}

  

startActivity:空白Activity
package com.example.kaoyanpai;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class startActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private List<Integer> list=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        //10秒后向登录界面跳转
        initViews();
        initData();
        Timer timer=new Timer();
        TimerTask timerTask=new TimerTask() {
            @Override
            public void run() {
                startActivity(new Intent(startActivity.this,zhuce.class));
            }
        };
        timer.schedule(timerTask,10000);
        Log.i("message","chenggong");
    }
    private void initViews() {  //加载布局

        viewPager = (ViewPager) findViewById(R.id.im1);
        viewPager.setAdapter(my);
    }

    private PagerAdapter my=new PagerAdapter(){

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view==object;
        }
        //Alt+Insert
        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            View item= LayoutInflater.from(container.getContext()).inflate(R.layout.item,container,false);
            ImageView iv=item.findViewById(R.id.im2);
            //设置数据

            iv.setImageResource(list.get(position));
            if(iv.getParent() instanceof ViewGroup){
                ((ViewGroup)iv.getParent()).removeView(iv);
            }
            container.addView(iv);
            return iv;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView((View)object);
        }
    };


    private void initData() {    //加载数据
        list.add(R.mipmap.yan1);
        list.add(R.mipmap.yan2);
        list.add(R.mipmap.yan3);
        my.notifyDataSetChanged();
    }
}

  zhuce:空白Activity

package com.example.kaoyanpai;

//import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class zhuce extends AppCompatActivity implements Button.OnClickListener {
private EditText name;
private EditText password;
private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);
        name=(EditText)findViewById(R.id.dengluname);
        password=(EditText)findViewById(R.id.denglupassword);
        button=(Button)findViewById(R.id.button2);
        button.setOnClickListener(this);
    }
    public void onClick(View view) {
        //登录或者注册
        String name1 = name.getText().toString();
        String password1 = password.getText().toString();
        List<CostBean> list = new ArrayList<CostBean>();
        DBService service = new DBService();
        //已有这个用户,提示
        if (service.getUserData(name1) == true) {
            showCoverDialog("登录成功");
        } else {//没有这个用户 添加
            CostBean costBean = new CostBean();
            costBean.setPassword(password1);
            costBean.setName(name1);
            list.add(costBean);
            if (list != null) {
                Log.i("list", name1);
                //添加成功,提示
                service.insertUserData(list);
                showCoverDialog("注册成功");
            } else {
                //添加失败 提示
                Log.i("list", "空值");
                showCoverDialog1("注册失败,请重新填写信息");
            }
        }
        showCoverDialog("成功");
    }
    //测试
    private void showCoverDialog(String message){
        final AlertDialog.Builder builder = new AlertDialog.Builder(zhuce.this);
        builder.setTitle("提示");
        builder.setMessage(message);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                startActivity(new Intent(zhuce.this,MainActivity.class));
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }
    private void showCoverDialog1(String message){
        final AlertDialog.Builder builder = new AlertDialog.Builder(zhuce.this);
        builder.setTitle("提示");
        builder.setMessage(message);
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }

}

  MianActivity:BottomNa。。。(带有底部导航栏)

package com.example.kaoyanpai;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications,R.id.shequ,R.id.wode)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

    }

}

HomeFragment:包下的UI文件夹

package com.example.kaoyanpai.ui.home;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.kaoyanpai.DBService;
import com.example.kaoyanpai.R;
import com.example.kaoyanpai.school;

import java.util.List;


public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private Button button;
    private EditText editText;
    private TextView textView;
    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Log.i("789","789");
            switch (msg.what){
                case 0x11:
                    String s = (String) msg.obj;
                    textView.setText(s);
                    Log.i("message","123");
                    break;
                case 0x12:
                    String ss = (String) msg.obj;
                    textView.setText(ss);
                    break;
            }

        }
    };
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@NonNull String s) {
                textView.setText(s);
            }
        });

        onStart(root);
        return root;

    }

    protected void onStart(View v){
          button=(Button)v.findViewById(R.id.button3);
          editText=(EditText)v.findViewById(R.id.schoolname);
          textView=(TextView)v.findViewById(R.id.show);


          button.setOnClickListener(new View.OnClickListener(){

              @Override
              public void onClick(View v) {
                  //创建一个线程,点击查询按钮,查询高校信息
                  new Thread(new Runnable() {
                      @Override
                      public void run() {
                          Looper.prepare();
                          //Handler handler=new Handler();
                          Message message=handler.obtainMessage();
                          /*message.what=0x11;
                          message.obj="chenggong";
                          Log.i("message","111");
                          handler.sendMessage(message);
                          Log.i("456","456");
                          */
                         String name=editText.getText().toString();
                          DBService service=new DBService();
                          List<school> list=service.searchschool(name);
                          if (list != null) {
                              String s = "";
                              for (int i = 0; i < list.size(); i++) {
                                  s += "分数线:" + list.get(i).getFenshu() + "\n";
                                  s += "报录比:" + list.get(i).getBili() + "\n";
                                  s += "经验:  " + list.get(i).getJingyan() + "\n";
                                  s += "加入讨论组  " + list.get(i).getTaolunzu()+ "\n";
                              }
                              //0x11、0x12消息的定位标志
                              message.what = 0x12;
                              message.obj = s;
                          } else {
                              message.what = 0x11;
                              message.obj = "查询结果为空";
                          }
                          handler.sendMessage(message);
                      }
                  }).start();
              }
          });
    }

}

  

 

 Layout部分:

 

 

 shequ.xml 以及wode.xml我都没写,fragment开头的是自动生成的

activity_start.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/pink"
    tools:context=".startActivity">
    <com.example.kaoyanpai.ScoViewParger
        android:id="@+id/im1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"/>
</RelativeLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/im2"/>
</LinearLayout>

activity_mian.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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_zhuce.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".zhuce">
<!--app图标区域-->


    <ImageView
        android:layout_marginLeft="150dp"
        android:id="@+id/tubiao"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="38dp"
        android:background="@color/colorAccent"
        android:src="@mipmap/touxiang"
        android:scaleType="fitXY"/>

    <TextView
            android:id="@+id/jvzi"
            android:layout_width="450px"
            android:layout_height="100px"
            android:layout_below="@+id/tubiao"
            android:layout_marginTop="38dp"
            android:gravity="center"
            android:text="让考研不再孤单" />


    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/denglu_name"
    android:text="姓名"
    android:layout_below="@+id/jvzi"
    android:layout_marginTop="50dp"
    android:layout_marginLeft="42dp"/>
<EditText
    android:layout_width="500px"
    android:layout_height="100px"
    android:id="@+id/dengluname"
    android:layout_below="@+id/jvzi"
    android:layout_toRightOf="@+id/denglu_name"
    android:layout_marginTop="50dp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/denglu_password"
    android:text="密码"
    android:layout_below="@+id/denglu_name"
    android:layout_marginTop="37dp"
    android:layout_marginLeft="42dp"/>
<EditText
    android:layout_width="500px"
    android:layout_height="100px"
    android:id="@+id/denglupassword"
    android:layout_below="@+id/dengluname"
    android:layout_toRightOf="@+id/denglu_password"
    android:layout_marginTop="37dp"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/denglupassword"
        android:id="@+id/button1"
        android:layout_marginTop="37dp"
        android:layout_marginLeft="42dp"
        android:text="同意研乎《用户注册协议》《隐私协议》" />
    <TextView
        android:layout_width="500px"
        android:layout_height="100px"
        android:id="@+id/jvzi2"
        android:layout_below="@+id/button1"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="30dp"
        android:text="新用户登录后自动注册"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/jvzi2"
        android:id="@+id/button2"
        android:layout_marginLeft="42dp"
        android:layout_marginTop="25dp"
        android:text="登录/注册"
        android:onClick="onClick"/>
</RelativeLayout>

fragment_home.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:actionBarNavMode="tabs">


        <TextView
            android:id="@+id/text_home"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="8dp"
            android:textAlignment="center"
            android:textSize="50sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/school_name"
            android:layout_width="100dp"
            android:layout_height="45dp"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginBottom="500dp"
            android:text="@string/school_name"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/text_home"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.93" />

        <EditText
            android:id="@+id/schoolname"
            android:layout_width="300dp"
            android:layout_height="45dp"
            android:layout_marginStart="12dp"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="156dp"
            android:hint="请输入您想要查询的学校信息"
            app:layout_constraintStart_toEndOf="@+id/school_name"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginStart="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginBottom="440dp"
            android:text="查询"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/show"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginBottom="172dp"
            android:text="123"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:layout_editor_absoluteX="-31dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

  values:string.xml

<resources>
    <string name="app_name">kaoyanpai</string>
    <string name="title_home">Home</string>
    <string name="title_dashboard">Dashboard</string>
    <string name="title_notifications">Notifications</string>
    <string name="school_name">院校名称</string>
</resources>

  values:color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="pink">#FFC0CB</color>
</resources>

  轮播图的照片复制到mipmap下

数据库表

用户表:

 

院校表:

 

python爬取数据代码:

import bs4
import pymysql
import requests
from bs4 import BeautifulSoup
import pandas as pd

def download_all_htmls():
    htmls=[];
    for idx in range(2):
     url=f"https://yz.chsi.com.cn/sch/?start={idx*20}"
     print("craw html",url)
     r=requests.get(url)
     if r.status_code!=200:
        raise Exception("error")
     htmls.append(r.text)
    return htmls
htmls=download_all_htmls()

def parse_single_html(html):
    name1=[];
    soup=BeautifulSoup(html,'html.parser')

    tbody=soup.find('tbody')
    print("1")

    if isinstance(tbody, bs4.element.Tag):
      for tr in tbody.find_all("tr"):
            a=tr.find("td").find("a")
            name1.append(a.getText().replace(' ', ''))
      print(name1)
      return name1
    '''
for idex in range(2):
        name2 = parse_single_html(htmls[idex])
       
         name=tbody.find('tr').find('td').find('a')
        '''

conn = pymysql.connect(
        host='localhost',  # 我的IP地址
        port=3306,  # 不是字符串不需要加引号。
        user='root',
        password='zhangziyi1670',
        db='yanhu',
        charset='utf8'
    )
cursor = conn.cursor()  # 获取一个光标
for idex in range(2):
  name = parse_single_html(htmls[idex])
  for a in range(20):
   sql = 'insert into school (name,fenshu,bili) values (%s,%s,%s);'
   name3=name[a]
   print(name3)
   fenshu=420
   bili=0.2
   cursor.execute(sql, [name3,fenshu,bili])
print()

conn.commit()

cursor.close()
conn.close()

# name2=parse_single_html(htmls[0])

# def pa(html):
#     soup=BeautifulSoup(html,"html.parser")
#     articles=soup.find_all("artical")
#     datas=[]
#     for article in articles:
#         title_node=(
#             article.find("h2",class_="entry_title")
#             .find("a")
#         )
#         title=title_node.get_text()
#         # 得到title的内容
#         link=title_node["href"]
#         # 得到title的链接
#         datas.append({"title":title,"link":link});
#         return datas

  

 

posted @ 2020-04-25 11:42  Protect_Winter  阅读(139)  评论(0编辑  收藏  举报