build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.adminjs.jiaosheng1212"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
compile 'com.squareup.retrofit2:converter-gson:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
compile 'io.reactivex:rxandroid:1.1.0'
}
Service接口
public interface Service {
///http://120.27.23.105/user/login
@GET("user/login")
Observable getLogin(@Query("mobile") String mobile, @Query("password") String passwprd);
@GET("user/reg")
Observable getRegist(@Query("mobile") String mobile, @Query("password") String passwprd);
//http://120.27.23.105/
@GET("user/getUserInfo")
Observable getUid(@Query("uid") String uid);
}
MainActivity
public class MainActivity extends AppCompatActivity {
private EditText phone;
private EditText pass;
private Button lu;
private Button ce;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phone = findViewById(R.id.phone);
pass = findViewById(R.id.pass);
lu = findViewById(R.id.login_lu);
ce = findViewById(R.id.login_ce);
lu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String p = phone.getText().toString().trim();
String pa = pass.getText().toString().trim();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://120.27.23.105")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
Service service = retrofit.create(Service.class);
service.getLogin(p,pa).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(LoginBean loginBean) {
String code = loginBean.getCode();
if (code.equals("0")){
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, XinxiActivity.class);
startActivity(intent);
}else{
Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
}
}
});
}
});
ce.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ZhuceActivity.class);
startActivity(intent);
}
});
}
}
ZhuceActivity
public class ZhuceActivity extends AppCompatActivity{
private EditText uphone;
private EditText upass;
private TextView fan;
private Button uzhu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhuce);
uphone = findViewById(R.id.uphone);
upass = findViewById(R.id.upass);
fan = findViewById(R.id.fan);
uzhu = findViewById(R.id.zhu);
fan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ZhuceActivity.this, MainActivity.class);
startActivity(intent);
}
});
uzhu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String p = uphone.getText().toString().trim();
String pa=upass.getText().toString().trim();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://120.27.23.105")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
Service service = retrofit.create(Service.class);
service.getRegist(p,pa).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(RegistBean registBean) {
String code = registBean.getCode();
if (code.equals("0")){
Toast.makeText(ZhuceActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
finish();
}else{
Toast.makeText(ZhuceActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
XinxiActivity
public class XinxiActivity extends AppCompatActivity{
private Button tui;
TextView username;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xinxi);
tui = findViewById(R.id.tui);
username = findViewById(R.id.username);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://120.27.23.105")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
Service service = retrofit.create(Service.class);
service.getUid(983+"").subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(XiangqingBean xiangqingBean) {
String name = xiangqingBean.getData().getUsername();
username.setText(name);
}
});
tui.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
清单列表
[github源代码:]https://github.com/jisheng6/Jiaosheng1212