Newer
Older
NemophilaClient / app / src / main / java / com / example / nemophila / SignUpActivity.java
package com.example.nemophila;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.nemophila.entities.Account;
import com.example.nemophila.entities.AccountJson;
import com.example.nemophila.resources.AccountsRest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

@JsonIgnoreProperties(ignoreUnknown=true)
public class SignUpActivity extends AppCompatActivity{

    private Retrofit retrofit;
    private AccountsRest AccountsRest;
    private Nemophila nemophila;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);

        //Nemophilaと連携
        nemophila = (Nemophila) this.getApplication();

        //通信の初期化
        this.retrofit = new Retrofit.Builder()
                .baseUrl("http://nitta-lab-www.is.konan-u.ac.jp/nemophila/")
                .addConverterFactory(JacksonConverterFactory.create())
                .build();
        this.AccountsRest = retrofit.create(AccountsRest.class);

//アカウント新規製作
        findViewById(R.id.SignUpButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Nameとpwを入力
                EditText editname = (EditText) findViewById(R.id.editTextTextUserName);
                String name = editname.getText().toString().trim();
                EditText editpw = (EditText) findViewById(R.id.editTextTextNewPassword);
                String pw = editpw.getText().toString().trim();

                if (!name.isEmpty() && !pw.isEmpty() && !name.trim().equals("") && !pw.trim().equals("")) {
                    // 通信
                    Call<AccountJson> call = AccountsRest.createAccounts(name, pw);

                    call.enqueue(new Callback<AccountJson>() {
                        @Override
                        public void onResponse(Call<AccountJson> call, Response<AccountJson> response) {
                            if (response.isSuccessful()) {
                                System.out.println("通信成功:changeBelongsAndMaster");

                                //nemophila(自端末)にname,id,tokenを記憶
                                nemophila.setName(response.body().getName());
                                nemophila.setUid(response.body().getId());
                                nemophila.setToken(response.body().getToken());

                                //mainに画面遷移
                                Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
                                startActivity(intent);
                            } else {
                                System.out.println("通信可能:changeBelongsAndMaster: " + response.code());
                                if (response.code() == 500) {
                                    ((TextView) findViewById(R.id.textView)).setText("The UserName you gave is already in use.");
                                } else if (response.code() == 404) {
                                    ((TextView) findViewById(R.id.textView)).setText("SignUp failed. Please enter the correct credentials.");
                                }
                            }
                        }

                        @Override
                        public void onFailure(Call<AccountJson> call, Throwable t) {
                            System.out.println("通信失敗:changeBelongsAndMaster");
                            System.out.println(t);
                        }
                    });
                }
            }
        });
//Login画面に遷移
        findViewById(R.id.LoginTrans).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SignUpActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
    }
}