본문 바로가기

자바 웹 개발자가 될거야/JAVA

[Android] JSP서버와 안드로이드 연동

< 로그인해서 서버 연동 >

 

 

① 안드로이드 로그인 화면

 

- 오른쪽 화면 메뉴 중 로그인이라는 메뉴를 눌렀을 때 로그인할 수 있는 창 등장

- 확인 버튼을 눌렀을 때 로그인한 서버 연결

 

public boolean onCreateOptionsMenu(Menu menu){
    super.onCreateOptionsMenu(menu);
    menu.add(0,1,0,"로그인");

    return true;
}

public boolean onOptionsItemSeleted(@NonNull MenuItem){
    super.onOptionItemSelected(item);
    switch(item.getItemId()){
        case 1:
            View dlgView = View.inflate(MainActivity.this, R.layout.dialog1,null);
            AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
            dlg.setTilte("로그인하기");
            dlg.setIcon(R.drawable.icon2);
            dlg.setView(dlgView);
            dlg.setNegativeButton("취소",null);
            dlg.setPositiveButton("확인",null);
            dlg.show();
            return true;
    }
    return false;
}

  · MainActivity 안에 추가

 

- res 폴더 아래 layout 폴더에 dialog1.xml 생성

 

아이디 LinearLayout
패스워드 LinearLayout

 

- 아이디, 패스워드의 EditText에 아이디 부여하기

 

 

- dialog.xml에서 패딩값 부여해서 디자인 좀 다듬기 .. ㅎ

 

 

 

② JSP와 서버 연결하기

 

 

- 이클립스로 들어가서 다이나믹 웹 프로젝트 생성

 

 

- 테스트용으로 JSP 파일에 코드 작성해봄 (html 태그들은 다 삭제하고 작성했음)

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<% String userId = request.getParameter("userid");
   String userPw = request.getParameter("userpw");
   String msg = "아이디 : " + userId + "패스워드 : "+userPw;
%>

<%=msg%>

 

- 확인 버튼을 눌렀을 때 동작할 수 있도록 dlg.setPositiveButton("확인",)에 클릭리스너 추가해서 작성

 

public boolean onOptionsItemSeleted(@NonNull MenuItem){
    switch(item.getItemId()){
        case 1:
        	...            
            dlg.setPositiveButton("확인",new DialogInterface.OnClickListener(){
            	@Override
            	public void onClick(DialogInterface dialogInterface, int i){
                    Thread t1 = new Thread(new Runnable() {
                        public void run(){
                            connect();
                        }
                    });
                    t1.start();                	
                }            
            });
            dlg.show();
            return true;
    }
    return false;
}

private void connect(){
    String urlPath = "http://나의 IP주소:8000/LoginTest/login.jsp?userid=yoon&userpw=1234";
	try{
        URL url = new URL(urlPath);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        if(con != null){
            con.setRequestMethod("GET");
            con.setDoInput(true);
            con.setDoOutput(true);
            int code = con.getResponseCode();
            Log.i("mytag","RESPONSE_CODE : "+code);
            
            InputStream input = con.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            StringBuffer buf = new StringBuffer();
            String str = reader.readLine();
            while(str != null){
            	buf.append(str);
                str = reader.readLine();
            }
            reader.close();
        }
        con.disconnect();
        
    }catch(Exception e){
        Log.i("mytag",e.getLocalizedMessage());
    }
}

 

 

- AndroidManifest에 인터넷 허용하기

 

테스트 연결 완료