我正在开发一款具有改进功能的应用程序。
我是新开发的改造登录。如何通过改造实施登录?
的登录我的JSON代码:
{
"login": [
{
"sessionid": 12973,
"responsetypes": "success"
}
]
}
如何实现这种类型的JSON的登录? 以下是我的代码:它给错误的反应method.how我解决这个问题?
公共类LoginActivity延伸AppCompatActivity实现View.OnClickListener {
private AppCompatButton btn_login;
private EditText et_email,et_password;
private TextView tv_register;
private ProgressBar progress;
private SharedPreferences pref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
pref = LoginActivity.this.getPreferences(0);
btn_login = (AppCompatButton)findViewById(R.id.btn_login);
tv_register = (TextView)findViewById(R.id.tv_register);
et_email = (EditText)findViewById(R.id.et_email);
et_password = (EditText)findViewById(R.id.et_password);
progress = (ProgressBar)findViewById(R.id.progress);
btn_login.setOnClickListener(this);
tv_register.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_register:
goToRegister();
break;
case R.id.btn_login:
String email = et_email.getText().toString();
String password = et_password.getText().toString();
if(!email.isEmpty() && !password.isEmpty()) {
progress.setVisibility(View.VISIBLE);
loginProcess(email,password);
} else {
Toast.makeText(LoginActivity.this, "Fields are empty !", Toast.LENGTH_LONG).show();
}
break;
}
}
private void loginProcess(String userID,String password){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
User user = new User();
user.setEmail(userID);
user.setPassword(password);
ServerRequest request = new ServerRequest();
request.setOperation(Constants.LOGIN_OPERATION);
request.setUser(user);
Call<ServerResponse> response = requestInterface.operation(request);
response.enqueue(new Callback<ServerResponse>() {
@Override
public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {
ServerResponse resp = response.body();
Toast.makeText(LoginActivity.this, resp.getMessage(), Toast.LENGTH_LONG).show();
if(resp.getResult().equals(Constants.SUCCESS)){
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN,true);
editor.putString(Constants.EMAIL,resp.getUser().getEmail());
// editor.putString(Constants.NAME,resp.getUser().getName());
editor.putString(Constants.UNIQUE_ID,resp.getUser().getUnique_id());
editor.apply();
goToProfile();
}
progress.setVisibility(View.INVISIBLE);
}
@Override
public void onFailure(Call<ServerResponse> call, Throwable t) {
progress.setVisibility(View.INVISIBLE);
Log.d(Constants.TAG,"failed");
//Snackbar.make(getV, t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
}
});
}
private void goToRegister(){
Intent i = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(i);
}
private void goToProfile(){
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
}
}
// 公共接口RequestInterface {
@POST("ypAndroid/api/")
Call<ServerResponse> operation(@Body ServerRequest request);
}
公共类常量{
public static final String BASE_URL = "http://www.example.com/";
public static final String REG_URL = "http://www.example.com/";
public static final String REGISTER_OPERATION = "register";
public static final String LOGIN_OPERATION = "login";
public static final String CHANGE_PASSWORD_OPERATION = "chgPass";
public static final String SUCCESS = "success";
public static final String FAILURE = "failure";
public static final String IS_LOGGED_IN = "isLoggedIn";
public static final String NAME = "name";
public static final String EMAIL = "userID";
public static final String UNIQUE_ID = "sessionid";
public static final String TAG = "Demo";
}
// 公共类ServerRequest {
private String operation;
private User user;
public void setOperation(String operation) {
this.operation = operation;
}
public void setUser(User user) {
this.user = user;
}
}
//公共类ServerResponse {
private String result;
private String message;
private User user;
public String getResult() {
return result;
}
public String getMessage() {
return message;
}
public User getUser() {
return user;
}
}
//
公共类登录{
@SerializedName("sessionid")
@Expose
private Integer sessionid;
@SerializedName("responsetypes")
@Expose
private String responsetypes;
public Integer getSessionid() {
return sessionid;
}
public void setSessionid(Integer sessionid) {
this.sessionid = sessionid;
}
public String getResponsetypes() {
return responsetypes;
}
public void setResponsetypes(String responsetypes) {
this.responsetypes = responsetypes;
}
}
分享您试图代码.... –
我加入了代码,请建议我? – Rohu
请发布ServerResponse的代码 –