package com.tistory.link2me.pdologin;
import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast;
import com.tistory.link2me.common.PHPComm;
import okhttp3.MediaType;
public class Register extends AppCompatActivity { private static final String TAG = Register.class.getSimpleName(); private Button btnRegister; private Button btnLogin; private EditText inputUserName; private EditText inputEmail; private EditText inputPassword; private EditText inputMobileNO; private EditText inputTelNO; private String userID; // SharedPreferences 저장 목적으로 Class 전역변수 선언
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); public SharedPreferences settings;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register);
inputUserName = (EditText) findViewById(R.id.name); inputEmail = (EditText) findViewById(R.id.email); inputPassword = (EditText) findViewById(R.id.password); inputMobileNO = (EditText) findViewById(R.id.mobileNO); inputTelNO = (EditText) findViewById(R.id.telNO); btnRegister = (Button) findViewById(R.id.btnRegister); btnLogin = (Button) findViewById(R.id.btnLoginScreen);
// 등록 버튼 btnRegister.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { String name = inputUserName.getText().toString().trim(); String email = inputEmail.getText().toString().trim(); userID = email; String password = inputPassword.getText().toString().trim(); String mobileNO = inputMobileNO.getText().toString().trim(); String telNO = inputTelNO.getText().toString().trim();
if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty() && !mobileNO.isEmpty()) { registerUser(name, email, password, mobileNO, telNO); } else { Toast.makeText(getApplicationContext(), "필수사항은 모두 입력하세요!", Toast.LENGTH_LONG).show(); } } });
// Login Screen 으로 Activity 이동 btnLogin.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { startActivity(new Intent(getApplicationContext(), Login.class)); finish(); } });
}
private void registerUser(String name, String userID, String password, String mobileNO, String telNO) { // 전달할 인자들 Uri.Builder builder = new Uri.Builder() .appendQueryParameter("name", name) .appendQueryParameter("userID", userID) .appendQueryParameter("email", userID) .appendQueryParameter("password", password) .appendQueryParameter("mobileNO", mobileNO) .appendQueryParameter("telNO", telNO); String urlParameters = builder.build().getEncodedQuery(); new getJSONData().execute(Value.IPADDRESS + "/register.php", urlParameters); }
private class getJSONData extends AsyncTask<String, Void, String> { ProgressDialog pdLoading = new ProgressDialog(Register.this);
@Override protected void onPreExecute() { super.onPreExecute(); pdLoading.setMessage("\t회원가입 처리중..."); pdLoading.setCancelable(false); pdLoading.show(); }
@Override protected String doInBackground(String... params) { try { return PHPComm.getJson(params[0],params[1]); } catch (Exception e) { return new String("Exception: " + e.getMessage()); } }
protected void onPostExecute(String result){ pdLoading.dismiss(); showJSONResult(result); } }
protected void showJSONResult(String result) { if(result.equalsIgnoreCase("1")){ Toast.makeText(this, "회원가입 완료", Toast.LENGTH_SHORT).show(); storeUserData(); startActivity(new Intent(getApplicationContext(), Login.class)); finish(); } else if(result.equalsIgnoreCase("-1")){ Toast.makeText(this, "아이디가 이미 가입되어 있습니다", Toast.LENGTH_SHORT).show(); inputEmail.clearFocus(); } else { Toast.makeText(this, "회원 등록에 문제가 발생했습니다", Toast.LENGTH_SHORT).show(); } }
private void storeUserData(){ settings = getSharedPreferences("settings", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putString("userID", userID); editor.putBoolean("autologin", true); editor.commit(); } }
|