diff --git a/app/src/main/java/com/example/citrusclient/views/LoginActivity.java b/app/src/main/java/com/example/citrusclient/views/LoginActivity.java index 3db4544..d38374d 100644 --- a/app/src/main/java/com/example/citrusclient/views/LoginActivity.java +++ b/app/src/main/java/com/example/citrusclient/views/LoginActivity.java @@ -2,18 +2,32 @@ import android.content.Intent; import android.os.Bundle; // +import android.os.Handler; import android.view.View; import android.widget.Button; +import android.widget.EditText; +import android.widget.TextView; import androidx.activity.EdgeToEdge; import androidx.appcompat.app.AppCompatActivity; // import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import com.example.citrusclient.Citrus; import com.example.citrusclient.R; +import com.example.citrusclient.rest.AccountsRest; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; +import retrofit2.converter.jackson.JacksonConverterFactory; public class LoginActivity extends AppCompatActivity { + private Retrofit retrofit; + private AccountsRest accountsRest; + private Citrus citrus; + //画面起動時 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -25,16 +39,80 @@ return insets; }); - // username = findViewById(R.id.username); - // password = findViewById(R.id.password); - // loginButton = findViewById(R.id.loginButton); + //citrusと連携 + citrus = (Citrus) this.getApplication(); + + //通信の初期化 + this.retrofit = new Retrofit.Builder() + .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/citrus/") + .addConverterFactory(JacksonConverterFactory.create()) + .build(); + this.accountsRest = retrofit.create(AccountsRest.class); Button loginButton = (Button)findViewById(R.id.loginButton); loginButton.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ - Intent intent = new Intent(LoginActivity.this,MainActivity.class); - startActivity(intent); + //テキストを文字列で認識 + EditText editid = (EditText) findViewById(R.id.username); + String id = editid.getText().toString().trim(); + EditText editpw = (EditText) findViewById(R.id.password); + String pw = editpw.getText().toString().trim(); + final Handler handler = new Handler(); + + //入力欄が空欄の時 + if(id.isEmpty() && pw.isEmpty() && id.trim().isEmpty() && pw.trim().isEmpty()){ + System.out.println("不適切入力"); + ((TextView)findViewById(R.id.textView_respons)).setText("適切に入力してください"); + + //Intent intent = new Intent(LoginActivity.this,MainActivity.class); + //startActivity(intent); + + }else if(!id.isEmpty() && !pw.isEmpty() && !id.trim().isEmpty() && !pw.trim().isEmpty() ){ + //入力欄が全て適切に入力された場合の処理 + Call call = accountsRest.login(id,pw); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()){ + if (!response.body().equals("null")) { + System.out.println("通信成功"); + //端末にtoken.id登録 + String token = response.body(); + citrus.setToken(token); + citrus.setAccountId(id); + + //画面遷移 + handler.post(new Runnable() { + @Override + public void run() { + Intent intent = new Intent(LoginActivity.this, + MainActivity.class); + startActivity(intent); + } + }); + }else{ + ((TextView)findViewById(R.id.textView_respons)).setText(("パスワードが間違っています")); + + } + + }else{ + //通信可能(ただしエラーが発生した場合) + //404がPW入力されてないときだけど、前で定義しているからいらないよね..? + System.out.println("通信可能"); + if(response.code()==404){ + ((TextView)findViewById(R.id.textView_respons)).setText(("IDが間違っています")); + } + } + } + //通信失敗 + @Override + public void onFailure(Call call, Throwable t) { + System.out.println("通信失敗"); + System.out.println(t); + } + }); + } } }); Button newButton = (Button)findViewById(R.id.newAccount);