안드로이드 16일차

ParkJinYoung·2022년 10월 28일
0
  • ✔✔✔ { } : Object , [ ] : Array

구글 FireBase

  • JSON은 하나의 테이블인데 용량이 클때 유용함
public class LoginActivity extends AppCompatActivity {
    EditText edt_id;
    EditText edt_pw;
    Button btn_login;
    Button btn_join;

    RequestQueue queue;
    StringRequest join_request;
    StringRequest login_request;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        btn_login = findViewById(R.id.btn_login);
        edt_id = findViewById(R.id.edt_id);
        edt_pw = findViewById(R.id.edt_pw);
        btn_join = findViewById(R.id.btn_join);

        // Volley 라이브러리 사용하여 서버랑 통신하기
        // 1. App에 인터넷 설정해주기 => Manifest.xml에서 설정 (permission, clear~)
        // 2. 서버 url 저장하기(=접속이 되는지 확인)
        String Join_url = "http://220.71.97.222:8081/MemberServer/JoinServlet";
        // 3. requestQueue 객체 생성하기 => 데이터 전송통로(임시저장소 : Buffer✔✔)
        queue = Volley.newRequestQueue(getApplicationContext());

        // 4. stringRequest 객체 생성하기 => 전송방식 , 서버 url, 응답시 일어날일 (Listener), 에러났을때 일어날 일(Listener)
        // get/post , url, new Response.Listener<String>(), new Response.ErrorListener()
        join_request = new StringRequest(Request.Method.POST, Join_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // 응답처리하는곳
                if (response.equals("1")){
                    //회원가입 성공
                    Toast.makeText(getApplicationContext(), "회원가입 성공", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(), "회원가입 실패", Toast.LENGTH_SHORT).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // 에러났을때 처리하는곳
            }
        })

        {
          // {} stringRequest 공간 할당
            // Alt + Insert => getParams()✔✔✔✔
            @Nullable
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                // 서버로 보낼 파라미터(변수, 데이터) 설정 해주는 곳
                Map<String,String> params = new HashMap<>();
                // 순서없이 집어넣는다
                // key값 : 서버랑 약속한 값(request.getParameter), Value : EditText 이름
                params.put("id",edt_id.getText().toString());
                params.put("pw",edt_pw.getText().toString());

                return params;//✔✔✔✔✔✔
            }

        };

        // 5. queue에 stringRequest 전송하기 => 회원가입 버튼 눌렀을때
        // 이벤트 Listener
        btn_join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                queue.add(join_request);
            }
        });

        //로그인 기능 구현하기
        // 1.App 인터넷설정 O
        // 2.로그인서버
        String login_url = "http://220.71.97.222:8081/MemberServer/LoginServlet";
        // 3.queue 생성하기 O
        // 4.StringRequest 생성하기 => url마다 한개씩 생성
        login_request= new StringRequest(Request.Method.POST, login_url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // Json Array
                if(response.equals("true")){
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(getApplicationContext(),"id와 pw를 다시 확인해주세요",Toast.LENGTH_SHORT).show();

                    //톰캣(서버)
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        })
        {
            @Nullable
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String,String> temp = new HashMap<>();
                // 순서없이 집어넣는다
                // key값 : 서버랑 약속한 값(request.getParameter), Value : EditText 이름
                temp.put("id",edt_id.getText().toString());
                temp.put("pw",edt_pw.getText().toString());

                return temp;
            }
        };
        // 5.queue에 StringRequest 연결하기 => 로그인 버튼 눌렀을때
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                queue.add(login_request);
                Intent intent_main=new Intent(LoginActivity.this,
                        MainActivity.class);
                startActivity(intent_main);
            }
        });

        // 6. 응답처리하기 => 성공했으면 MainActivity로 전환 => "true","false"
        // 반정형인 Json(테이블 => String(문자) 기반으로한 으로 바꾼 데이터)을 사용하여 bit
        // DTO , VO를


    }
}
profile
꾸준히

0개의 댓글