一个简单的界面
这里也是调用后台
public class RetrofitClient {
public static String IP="http://218.7.112.123:10001/";
private static Retrofit retrofit=new Retrofit.Builder()
.baseUrl(IP)
.addConverterFactory(GsonConverterFactory.create())
.build();
public RetrofitClient(){
}
public static apiService apiService(){
return retrofit.create(apiService.class);
}
}
实现接口
@POST("/prod-api/api/login")
Call getLoginData(@Body LoginBean loginBean);
获取输入框内容,然后去调用登录接口,接下来去获取用户id,这里就要单独创建一个getUserId的类去实现了,在这个类里面调用用户信息接口,代码如下
package com.example.smartcity.Activity;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.TokenWatcher;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.smartcity.Bean.LoginBean;
import com.example.smartcity.Bean.MsgBean;
import com.example.smartcity.Bean.UserInfoBean;
import com.example.smartcity.R;
import com.example.smartcity.network.RetrofitClient;
import com.example.smartcity.network.Utils;
import java.util.Collections;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class LoginActivty extends AppCompatActivity {
private static final String TAG = "LoginActivty";
private EditText loginEdName;
private EditText loginEdPassword;
private Button loginBut;
private Button reBut;
private SharedPreferences user;
private String token;
private String name;
private String password;
private Button loginRegiter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_activty);
initView();
user = getSharedPreferences("user", MODE_PRIVATE);
loginBut.setOnClickListener(v -> {
String username = loginEdName.getText().toString().trim();
String password = loginEdPassword.getText().toString();
LoginBean loginBean = new LoginBean(username, password);
RetrofitClient.apiService().getLoginData(loginBean).enqueue(new Callback() {
@SuppressLint("CommitPrefEdits")
@Override
public void onResponse(Call call, Response response) {
MsgBean body = response.body();
if (body.getCode()==200&&body!=null) {
String token = body.getToken();
SharedPreferences.Editor edit = user.edit();
edit.putString("username",username);
edit.putString("password",password);
edit.putString("userId","");
edit.putString("token",token);
edit.apply();
Utils.login=1;
getUserId();
finish();
}else {
Toast.makeText(LoginActivty.this,body.getMsg(),Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call call, Throwable throwable) {
}
});
});
}
private void getUserId() {
RetrofitClient.apiService().getUserInoFoData(user.getString("token","")).enqueue(new Callback() {
@SuppressLint("CommitPrefEdits")
@Override
public void onResponse(Call call, Response response) {
UserInfoBean body = response.body();
if (body.getCode()==200&body!=null) {
UserInfoBean.UserBean user1 = body.getUser();
int userId = user1.getUserId();
SharedPreferences.Editor edit = user.edit();
edit.putString("username","");
edit.putString("password","");
edit.putString("userId",Integer.toString(userId));
edit.apply();
Toast.makeText(LoginActivty.this,"登陆成功",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call call, Throwable throwable) {
}
});
}
private void initView() {
loginEdName = (EditText) findViewById(R.id.login_ed_name);
loginEdPassword = (EditText) findViewById(R.id.login_ed_password);
loginBut = (Button) findViewById(R.id.login_but);
}
}



