onResponse中的改进2.0条件逻辑不起作用

时间:2018-04-30 08:30:42

标签: retrofit2

我正在尝试使用改装后的请求制作登录逻辑,这是我的代码

Button btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showProgress(true,"Sedang login... ");
                String username = inputUsername.getText().toString();
                String password = inputPassword.getText().toString();
                apiService.login(new LoginParam(username,password)).enqueue(new Callback<AuthResponse>() {
                    @Override
                    public void onResponse(Call<AuthResponse> call, Response<AuthResponse> response) {
                        if(response.isSuccessful()){
                            showProgress(false,null);
                            Editor ed = sp.edit();
                            ed.putString("token",response.body().getToken());
                            Boolean login = response.body().getLogin();
                            Log.d("Login",response.body().getLogin().toString());
                            ed.putBoolean("login",response.body().getLogin().booleanValue());
                            ed.putString("username",response.body().getUser().getUsername());
                            ed.commit();
                            //startActivity(new Intent(getBaseContext(), MainActivity.class));
                            if(!login){
                                TextView infologin = (TextView) findViewById(R.id.loginInfo);
                                infologin.setText("Username dan password salah, coba lagi");
                                infologin.setVisibility(View.VISIBLE);
                                return;
                            }
                            else{
                                Intent i = new Intent(getBaseContext(),MainActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                i.putExtra("route","afterlogin");
                                startActivity(i);
                                finish();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<AuthResponse> call, Throwable t) {
                        LoginActivity.this.showProgress(false, null);
                        Toast.makeText(LoginActivity.this, "Gagal koneksi ke server, periksa jaringan internet anda error: " + t.toString(), Toast.LENGTH_LONG).show();
                    }
                });

            }
        });

当用户名和密码正确时它正在工作,但是当它没有时,它只显示加载动画,如果登录为false则处理的块仍然不受影响。出了什么问题?

0 个答案:

没有答案