1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| package com.example.myapplication1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast;
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;
public class MainActivity extends AppCompatActivity { TextView et1,et2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et1=(TextView)this.findViewById(R.id.et1); et2=(TextView)this.findViewById(R.id.et2);
Button bt1=(Button)this.findViewById(R.id.bt1); Button bt2=(Button)this.findViewById(R.id.bt2); } public void register(View view){ Intent intent = new Intent(MainActivity.this, RegisterActivity.class); startActivity(intent); } public void sign(View view) { String ac = et1.getText().toString(); String pw = et2.getText().toString(); Log.e("账号", ac); Log.e("密码", pw); if (!ac.equals("") && !pw.equals("")) { // Toast.makeText(getApplication(),"用户名密码正常",Toast.LENGTH_SHORT).show(); new Thread(new Runnable() { @Override public void run() { try { String ac = et1.getText().toString(); String pw = et2.getText().toString(); Log.e("账号", ac); Log.e("密码", pw); String str = "http://www.tomatobz.xyz:3000/login?Accout=" + ac + "&Password=" + pw; URL url = new URL(str); Log.e("url", str); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setReadTimeout(5000); httpURLConnection.setConnectTimeout(5000); InputStream inputStream = httpURLConnection.getInputStream(); InputStream in = url.openStream();//url非常重要的方法 openStream()返回输入流 用于读取网页流内容 BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line = ""; String result = ""; while ((line = br.readLine()) != null) { result += line; Log.e("返回结果", result); } if (result.equals("success")) { //通知主线程更新 makeToastByHandlerSendMessage("登录成功"); Intent intent = new Intent(MainActivity.this, home.class); startActivity(intent); } else { //通知主线程更新 makeToastByHandlerSendMessage("账号或密码出错"); } br.reset(); br.close(); } catch (Exception e) { e.printStackTrace(); } } }).start(); } else{ Toast.makeText(getApplication(),"用户名或密码不能为空",Toast.LENGTH_SHORT).show(); } }
static final int SUCCESS=1; static final int FAIL=0; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case SUCCESS: Toast.makeText(MainActivity.this, msg.getData().get("msg").toString(), Toast.LENGTH_SHORT).show(); break; case FAIL: Toast.makeText(MainActivity.this, msg.getData().get("msg").toString(), Toast.LENGTH_SHORT).show(); break; default: super.handleMessage(msg); } } };
private void makeToastByHandlerSendMessage(String msgStr) { Message msg = new Message(); msg.what = 0; Bundle bundle = new Bundle(); bundle.putString("msg", msgStr); msg.setData(bundle); handler.sendMessage(msg); } }
|