login.xml
register.xml
values/string.xml
测试登录
Login
Register
Username
Password
Great, you\'ve logged in!
AndroidMainfest.xml
MainActivity.java
package com.fzq;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import com.Config;
import com.Utils;
import com.fzq.myapplicationt2.R;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText password;
private UserLoginTask mAuthTask = null;
private boolean hadLogin = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//login button
findViewById(R.id.loginButton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = username.getText().toString();
String pass = password.getText().toString();
mAuthTask = new UserLoginTask(user, pass);
mAuthTask.execute((Void) null);
}
});
username = findViewById(R.id.username);
password = findViewById(R.id.password);
//register button
findViewById(R.id.password).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//提示框确定是否跳转
new AlertDialog.Builder(MainActivity.this).setTitle("Jump").setMessage("Ready to jump?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, Register.class);
startActivity(intent);
}
})
.setNegativeButton("No", null)
.show();
}
});
}
/**
* Represents an asynchronous login/registration task used to authenticate
* the user.
*/
public class UserLoginTask extends AsyncTask {
private final String username;
private final String password;
public UserLoginTask(String username, String password) {
this.username = username;
this.password = password;
}
@Override
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.
try {
// Config.serverIp=serverHostView.getText().toString();
// Config.serverPort=Integer.parseInt(serverPortView.getText().toString());
// controller路径+参数
URL url = new URL(Config.getServerUri() + "/casemgr/control/ajaxLogin?USERNAME=" + username + "¤tPassword=" + password);
URLConnection conn = url.openConnection();
conn.connect();
InputStream inputStream = conn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
return false;
}
// TODO: register the new account here.
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
if (success) {
MainActivity.this.hadLogin = true;
Utils.toast(MainActivity.this, "登陆成功");
/*
第一个参数:上下文对象this
第二个参数:目标文件
*/
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
} else {
MainActivity.this.hadLogin = false;
Utils.toast(MainActivity.this, "登陆失败");
}
}
@Override
protected void onCancelled() {
mAuthTask = null;
}
}
}
LoginActivity.java
package com.fzq;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import com.SerialPortUtil;
import com.SerialUtil;
import com.fzq.myapplicationt2.R;
import com.mq.MqCommand;
import com.mq.MqttListener;
import com.mq.MqttManager;
import com.util.Debug;
import org.fusesource.mqtt.client.Callback;
import java.util.Map;
public class LoginActivity extends AppCompatActivity {
public static final String tag = LoginActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// lockerListener();
}
/*
public void lockerListener() {
MqttListener listener =null;
try {
listener = MqttManager.getListener("locker" + "listener");
// System.out.println(listener);
} catch (Exception e) {
e.printStackTrace();
}
Debug.logInfo("开始监听服务器命令", tag);
// final SerialPortUtil serialPortUtil = new SerialPortUtil("ttyUSB0", 9600, null);
listener.addReceiveHandlers(MqCommand.commandTopic, new Callback() {
@Override
public void onSuccess(Object o) {
Map map= SerialUtil.deSerial((byte[]) o);
// serialPortUtil.sendSerialPort((byte[]) map.get("hardwareId"));
new AlertDialog.Builder(LoginActivity.this).setTitle("Success!")
.setMessage("监听到的数据:"+map.get("hardwareId"))
.setNegativeButton("OK",null)
.show();
}
@Override
public void onFailure(Throwable throwable) {
}
});
try {
listener.start();
} catch (Exception e) {
e.printStackTrace();
}
}
*/
}
Login.java
package com.fzq;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import com.SerialPortUtil;
import com.SerialUtil;
import com.fzq.myapplicationt2.R;
import com.mq.MqCommand;
import com.mq.MqttListener;
import com.mq.MqttManager;
import com.util.Debug;
import org.fusesource.mqtt.client.Callback;
import java.util.Map;
public class LoginActivity extends AppCompatActivity {
public static final String tag = LoginActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
// lockerListener();
}
/*
public void lockerListener() {
MqttListener listener =null;
try {
listener = MqttManager.getListener("locker" + "listener");
// System.out.println(listener);
} catch (Exception e) {
e.printStackTrace();
}
Debug.logInfo("开始监听服务器命令", tag);
// final SerialPortUtil serialPortUtil = new SerialPortUtil("ttyUSB0", 9600, null);
listener.addReceiveHandlers(MqCommand.commandTopic, new Callback() {
@Override
public void onSuccess(Object o) {
Map map= SerialUtil.deSerial((byte[]) o);
// serialPortUtil.sendSerialPort((byte[]) map.get("hardwareId"));
new AlertDialog.Builder(LoginActivity.this).setTitle("Success!")
.setMessage("监听到的数据:"+map.get("hardwareId"))
.setNegativeButton("OK",null)
.show();
}
@Override
public void onFailure(Throwable throwable) {
}
});
try {
listener.start();
} catch (Exception e) {
e.printStackTrace();
}
}
*/
}
Register.java
package com.fzq;
import android.app.Activity;
import android.os.Bundle;
import com.fzq.myapplicationt2.R;
public class Register extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
}
}