2024.3.22
第十四天
所花时间:2小时
代码量:400+
博客量:1
了解到的知识点:页面布局部分和数据库操作部分
package com.hui.kechengtianjia; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class UserService { public static boolean signIn(String classname, String teacher,String place) { MyThread myThread = new MyThread("http://192.168.43.157:8080/cx/SignIn",classname,teacher,place); try { myThread.start(); myThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } return myThread.getResult(); } public static boolean signUp(String classname, String teacher,String place) { MyThread myThread = new MyThread("http://192.168.43.157:8080/cx/SignUp",classname,teacher,place); try { myThread.start(); myThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } return myThread.getResult(); } } class MyThread extends Thread { private String path; private String classname; private String teacher; private String place; private boolean result = false; public MyThread(String path,String classname,String teacher,String place) { this.path = path; this.classname = classname; this.teacher = teacher; this.place = place; } @Override public void run() { try { URL url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(8000);//设置连接超时时间 httpURLConnection.setReadTimeout(8000);//设置读取超时时间 httpURLConnection.setRequestMethod("POST");//设置请求方法,post String data = "classname=" + URLEncoder.encode(classname, "utf-8") + "&teacher=" + URLEncoder.encode(teacher, "utf-8")+ "&place=" + URLEncoder.encode(place, "utf-8");//设置数据 httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置响应类型 httpURLConnection.setRequestProperty("Content-Length", data.length() + "");//设置内容长度 httpURLConnection.setDoOutput(true);//允许输出 OutputStream outputStream = httpURLConnection.getOutputStream(); outputStream.write(data.getBytes("utf-8"));//写入数据 result = (httpURLConnection.getResponseCode() == 200); } catch (Exception e) { e.printStackTrace(); } } public boolean getResult() { return result; } } // MyThread myThread = new MyThread("http://192.168.43.157:8080/cx/SignUp",classname,teacher,place); // MyThread myThread = new MyThread("http://192.168.43.157:8080/cx/SignIn",classname,teacher,place);
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="课程名称" /> <EditText android:layout_width="300dp" android:layout_height="60dp" android:id="@+id/etclass" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="任课教师" /> <EditText android:layout_width="300dp" android:layout_height="60dp" android:id="@+id/etteacher" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上课地点" /> <EditText android:layout_width="300dp" android:layout_height="60dp" android:id="@+id/etplace" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="120dp" android:layout_height="60dp" android:text="添加" android:id="@+id/signup" /> <Button android:layout_width="120dp" android:layout_height="60dp" android:text="登录" android:id="@+id/signin" /> </LinearLayout> </LinearLayout>