안드로이드

    

 

  

 

    

 

17강 변경 및 추가 부분

 

R.layout.activity_management

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#d1d1d1">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_marginTop="10dp"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:dividerHeight="10dp"
        android:divider="#d1d1d1">


    </ListView>






</LinearLayout>

 

 

user.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="#ffffff"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userID"
        android:text="아이디"
        android:textSize="20dp"
        android:textColor="#00a7f5"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userPassword"
        android:text="비밀번호"
        android:textSize="15dp"
        android:textColor="#f92a3f"
        />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userName"
        android:text="이름"
        android:textSize="26dp"
        android:textColor="#000000"
        />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userAge"
        android:text="나이"
        android:textSize="18dp"
        android:textColor="#775533"
        />





</LinearLayout>

 

class UserListAdapter

package kr.co.dothome.braverokmc.managerment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by choi on 2017-05-08.
 */

public class UserListAdapter extends BaseAdapter {

    Context mContext;
    List<MemberDTO> memberList;
    LayoutInflater inflater;

    public UserListAdapter(Context mContext, List<MemberDTO> memberList) {
        this.mContext = mContext;
        this.memberList = memberList;
        inflater=(LayoutInflater)mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return memberList.size();
    }

    @Override
    public Object getItem(int position) {
        return memberList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView==null){
            convertView=inflater.inflate(R.layout.user, parent, false);
        }

        TextView userID =(TextView)convertView.findViewById(R.id.userID);
        TextView userName =(TextView)convertView.findViewById(R.id.userName);
        TextView userPassword=(TextView)convertView.findViewById(R.id.userPassword);
        TextView userAge =(TextView)convertView.findViewById(R.id.userAge);


        userID.setText("아이디 : " +memberList.get(position).getUserID());
        userName.setText("이름 : " +memberList.get(position).getUserName());
        userPassword.setText("패스워드 : "+ memberList.get(position).getUserPassword());
        userAge.setText("나이 : " + memberList.get(position).getUserAge());

        if((position%2)==1){
          //  convertView.setBackgroundColor(0xffff8800);

        }else {
            convertView.setBackgroundColor(0xff748790);
        }

        return convertView;
    }




}




 

 

class ManagementActivity

package kr.co.dothome.braverokmc.managerment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;

public class ManagementActivity extends AppCompatActivity {

    //TextView userListTextView;
    ListView listView;


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

      //  userListTextView=(TextView)findViewById(userListTextView);
        listView=(ListView)findViewById(R.id.listView);


        Intent intent=getIntent();
        if(intent!=null) {

            Bundle membersBundle = intent.getBundleExtra("members");
            ArrayList<MemberDTO> membsers = (ArrayList<MemberDTO>) membersBundle.get("members");


            UserListAdapter adapter =new UserListAdapter(ManagementActivity.this, membsers);
            listView.setAdapter(adapter);

/*
            StringBuilder builder = new StringBuilder();
            for (MemberDTO dto : membsers) {

                builder.append("아이디 : "  + dto.getUserID()+"\n");
                builder.append( "이름 : " + dto.getUserName() +"\n");
                builder.append("패스워드 : " + dto.getUserPassword() +"\n");
                builder.append("나이 : " +dto.getUserAge() + "\n");
                builder.append("--------------------------------------\n\n\n");

            }
*/


        }


    }




}







 

 

  전체   

 

create table `USER` (

    userID varchar(20) not null,
    userPassword  varchar(20) not null,
    userName varchar(20) not null,
    userAge int not null,
    primary key (userID)


);

create table braverokmc2.`USER` ( userID varchar(20) not null, userPassword varchar(20) not null, userName varchar(20) not null, userAge int not null, primary key (userID) ) 

insert into braverokmc2.`USER` ( userID , userPassword , userName , userAge ) values( 'hong', '1111', '홍길동' , 39) 

select * from user where userID = 'hong' and userPassword = '1111' 

 

 

 

Login.php



<?php 
//http://braverokmc2.dothome.co.kr/Login.php?userID=hong&userPassword=1111


/* mysql_connect('localhost', 'mysql_user', 'mysql_password'); */
	$con =mysql_connect("localhost", "braverokmc2", "ehtjrhks5382");
	

	/* 	3. Mysql 입출력 인코딩을 지정
	 Mysql 연동을 한다면 입출력 인코딩이 달라서가 원인이 될 수 있다고 합니다.
	아래 3줄의 소스를 추가함으로써 해결하였습니다.
	
	출처: http://jhrun.tistory.com/140 [JHRunning] */
	
	mysql_query("set session character_set_connection=utf8;");
	
	mysql_query("set session character_set_results=utf8;");
	
	mysql_query("set session character_set_client=utf8;");
	
	
	mysql_set_charset("utf8");//sql 에서 한글 깨짐
	
	if($con==true){
	//	echo "yes 연결 되었습니다.";
		
		$_POST = array_map('mysql_escape_string', $_POST);
		$_GET=array_map('mysql_escape_string', $_GET);
		
		if(isset($_GET["userID"])){
			$userID =$_GET["userID"];
			$userPassword =$_GET["userPassword"];
			
		}else if(isset($_POST["userID"])){
			$userID =$_POST["userID"];
			$userPassword =$_POST["userPassword"];
		}

		
		if(isset($userID) && isset($userPassword)){
			
				
				$stmt = "select * from braverokmc2.USER where `userID` = '$userID' and `userPassword` = '$userPassword' ";
				$rs =mysql_query($stmt, $con);
				
				if($rs === FALSE) {
					die(mysql_error()); // TODO: better error handling
				}
				
			
	/* 			
				echo "<table border='1' >";
				echo
					"<tr>
				<th>번호</th>
				<th>이름</th>
				<th>내용</th>
				<th>이메일</th>
				<th>날짜</th>
				</tr>
				"; */
					
				$response =array();
				$response["success"]=false;
				
			if($row=mysql_fetch_array($rs)){
				
				$response["success"]=true;
				$response["userID"]=$row[userID];
				$response["userPassword"]=$row[userPassword];
				$response["userName"]=$row[userName];
				$response["userAge"]=$row[userAge];
			/* 	echo "
				<tr>
				<td>$row[userID]</td>
				<td>$row[userPassword] </td>
				<td>$row[userName] </td>
				<td>$row[userAge]</td>
				<td></td>
					
				</tr>"; */
				
			}
			
			
			echo json_encode($response);

			mysql_close();
			
			
		}
		

	}else{
		//echo "no - 연결 실패 하였습습니다.";
	}
	
	

	
	


?>







 

=>샘플 출력

{
success: true,
userID: "hong",
userPassword: "1111",
userName: "홍길동",
userAge: "39"
}

 

 

 

 

 

 

 

 

 

Register.php

<?php 

/* http://braverokmc2.dothome.co.kr/Register.php?userID=q1&userPassword=1111&userName=김길&userAge=89 */

	$con =mysql_connect("localhost", "braverokmc2", "ehtjrhks5382");
	
	
/* 	3. Mysql 입출력 인코딩을 지정
	Mysql 연동을 한다면 입출력 인코딩이 달라서가 원인이 될 수 있다고 합니다.
	아래 3줄의 소스를 추가함으로써 해결하였습니다.
	
	출처: http://jhrun.tistory.com/140 [JHRunning] */
	
	mysql_query("set session character_set_connection=utf8;");
	
	mysql_query("set session character_set_results=utf8;");
	
	mysql_query("set session character_set_client=utf8;");
	
	
	
	
	
	mysql_set_charset("SET NAMES utf8");
	if($con==true){
	
		//echo "연결";

		//Injection 방어
		$_POST = array_map('mysql_escape_string', $_POST);
		$_GET=array_map('mysql_escape_string', $_GET);
		
		if(isset($_POST["userID"])){
			$userID =$_POST["userID"];
			$userPassword=$_POST["userPassword"];
			$userName=$_POST["userName"];
			$userAge=$_POST["userAge"];
			
		}else if(isset($_GET["userID"])	){
			$userID =$_GET["userID"];
			$userPassword=$_GET["userPassword"];
			$userName=$_GET["userName"];
			$userAge=$_GET["userAge"];
		}

	/* 	//한글 인코딩 확인 후 utf8 아니 면 utf8 로 변경
		$enc=mb_detect_encoding($userName, array("UTF-8", "EUC-KR", "SJIS"));
		if($userName !="UTF-8"){
			$userName=iconv($enc, "UTF-8", $userName);
		}	
		
		 *
		 *
		 */
		$stmt= "insert into braverokmc2.USER values ( '$userID', '$userPassword', '$userName' , $userAge  ) ";
		mysql_query( $stmt);
		
		
		
	/* 	mysqli_stmt_bind_param($statement, "sssi", $userID, $userPassword, $userName, $userAge);
		mysqli_stmt_execute($statement);
		 */
		$response=array();
		$response["success"] =true;
		
		//echo $userName;
		echo json_encode($response);

	}

?>



 

=>

샘플 출력

{
success: true
}

 

 

 

=>  LoginRequest 클래스 추가    

 

APP

  compile 'com.android.volley:volley:1.0.0'

 

 

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.volley:volley:1.0.0'
    testCompile 'junit:junit:4.12'
}

 

AndroidMainfest.xml

  <uses-permission android:name="android.permission.INTERNET"/>

 

class LoginActivity

package kr.co.dothome.braverokmc.managerment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

//로그인 초기 화면
public class LoginActivity extends AppCompatActivity {


    EditText idText, passwordText;
    Button loginButton;

    TextView registerBtn;

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


        idText =(EditText)findViewById(R.id.idText);
        passwordText =(EditText)findViewById(R.id.pswordText);
        registerBtn =(TextView)findViewById(R.id.registerBtn);
        loginButton=(Button)findViewById(R.id.loginButton);



        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });


        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String userID=idText.getText().toString();
                final String userPassword=passwordText.getText().toString();


                Response.Listener<String> responseListenser =new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        try{

                            JSONObject jsoneRessponse=new JSONObject(response);
                            boolean succcess=jsoneRessponse.getBoolean("success");
                            if(succcess){
                                String userID= jsoneRessponse.getString("userID");
                                String userPassword=jsoneRessponse.getString("userPassword");
                                Intent intent=new Intent(LoginActivity.this, MainActivity.class);
                                intent.putExtra("userID", userID);
                                intent.putExtra("userPassword", userPassword);
                                LoginActivity.this.startActivity(intent);
                            }else{
                                AlertDialog.Builder builder=new AlertDialog.Builder(LoginActivity.this);
                                builder.setMessage("로그인에 실패하였습니다.");
                                builder.setNegativeButton("다시 시도", null);
                                builder.create();
                                builder.show();
                            }

                        }catch(Exception e){

                        }

                    }
                };

                LoginRequest loginRequest= new LoginRequest(userID, userPassword, responseListenser);
                RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
                queue.add(loginRequest);



            }
        });

    }
}









 

class LoginRequest  =>추가된 클래스

package kr.co.dothome.braverokmc.managerment;


import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by choi on 2017-05-02.
 */

public class LoginRequest extends StringRequest {

    final static private String URL="http://braverokmc2.dothome.co.kr/Login.php";

    private Map<String, String> parameters;

/*    public RegisterRequest(int method, String url,
                           Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
    }*/

    public LoginRequest(String userID, String userPassword,
                        Response.Listener<String> listener){

        super(Method.POST, URL, listener, null);
        try {
            //한글 깨짐 방지
          //  String st=URLEncoder.encode(userName, "UTF-8");

            parameters=new HashMap<>();
            parameters.put("userID", userID);
            parameters.put("userPassword", userPassword);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    @Override
    protected Map<String, String> getParams() {
        return parameters;
    }


}



 

 

 

 

class MainActivity

package kr.co.dothome.braverokmc.managerment;

import android.content.Intent;
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.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


//로그인 성공시
public class MainActivity extends AppCompatActivity {


    TextView idText, pswordText, welcome;
    Button manageMentButton;
    String userID, userPassword,message;
    ArrayList<MemberDTO> members ;


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

        idText =(TextView)findViewById(R.id.idText);
        pswordText =(TextView)findViewById(R.id.pswordText);
        welcome=(TextView)findViewById(R.id.welcome);

        manageMentButton=(Button)findViewById(R.id.manageMentButton);


        manageMentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new BackgroundTask().execute();
            }
        });


        Intent intent=getIntent();
        if(intent!=null){
            userID=intent.getStringExtra("userID");
            userPassword=intent.getStringExtra("userPassword");
            message ="환영 합니다. " + userID + "님!";

            idText.setText(userID);
            pswordText.setText(userPassword);
            welcome.setText(message);
        }


        if(!userID.equals("admin")){
            manageMentButton.setVisibility(View.GONE);
        }


    }


    //실제 회원 목록 가져오기,, AsyncTask 이용 또는 스레드 사용

    class BackgroundTask extends AsyncTask<Void, Void,  String>{

        String target;

        @Override
        protected void onPreExecute() {
            target="http://braverokmc2.dothome.co.kr/List.php";
        }


        @Override
        protected String doInBackground(Void... params) {

            InputStream is=null;
            InputStreamReader isr =null;
            BufferedReader reader=null;
            StringBuffer stringBuffer =new StringBuffer();


            try{

                URL url =new URL(target);
                HttpURLConnection httpURLConnection =(HttpURLConnection)url.openConnection();

                httpURLConnection.setConnectTimeout(10000);
                if(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){

                    is=httpURLConnection.getInputStream();
                    reader=new BufferedReader(new InputStreamReader(is));

                    while(true){
                        String stringLine =reader.readLine();
                        if(stringLine==null)break;
                        stringBuffer.append(stringLine+"\n");
                    }

                }

                parsing(stringBuffer.toString());
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    if(reader!=null) reader.close();
                    if(isr!=null) isr.close();
                    if(is!=null)is.close();

                }catch (Exception e){
                    e.printStackTrace();
                }

            }
            return null;
        }


        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }


        @Override
        protected void onPostExecute(String s) {

            Intent intent =new Intent(MainActivity.this, ManagementActivity.class);

            Bundle bundle =new Bundle();
            bundle.putSerializable("members", members);
            intent.putExtra("members",  bundle);
            startActivity(intent);

        }

    }


    public void parsing(String data){

        members=new ArrayList<>();
        try{
            JSONObject jsonObject =new JSONObject(data);
            JSONArray jsonArray=new JSONArray(jsonObject.getString("response"));

            //arrayList 클리어
            members.clear();

            for(int i=0; i<jsonArray.length(); i++){

                MemberDTO member=new MemberDTO();

                JSONObject jsonObject1=(JSONObject)jsonArray.get(i);
                member.setUserID(jsonObject1.getString("userID"));
                member.setUserName(jsonObject1.getString("userName"));
                member.setUserPassword(jsonObject1.getString("userPassword"));
                member.setUserAge(jsonObject1.getInt("userAge"));

                members.add(member);
            }


        }catch (Exception e){
            e.printStackTrace();
        }

    }





}

 

class RegisterActivity

package kr.co.dothome.braverokmc.managerment;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.util.regex.Pattern;

public class RegisterActivity extends AppCompatActivity {

    EditText idText, pswordText, nameText, ageText;
    Button registerButton;

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

        idText =(EditText)findViewById(R.id.idText);
        pswordText =(EditText)findViewById(R.id.pswordText);
        nameText =(EditText)findViewById(R.id.nameText);
        ageText =(EditText)findViewById(R.id.ageText);

        idText.setFilters(new InputFilter[] {filter});



        registerButton=(Button)findViewById(R.id.registerbutton);

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userID=idText.getText().toString();
                String userPassword=pswordText.getText().toString();
                String userName=nameText.getText().toString();
                int userAge=Integer.parseInt(ageText.getText().toString());

                Response.Listener<String> responseListener=new Response.Listener<String>(){

                    @Override
                    public void onResponse(String response) {
                        try{
                            JSONObject jsonResponse=new JSONObject(response);
                            boolean success=jsonResponse.getBoolean("success");

                            if(success){
                                AlertDialog.Builder builder=new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("회원 등록에 성공했습니다.")
                                        .setPositiveButton("확인", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                Intent intent=new Intent(RegisterActivity.this, LoginActivity.class);
                                                RegisterActivity.this.startActivity(intent);
                                            }
                                        })
                                        .create()
                                        .show();

                            }else{
                                AlertDialog.Builder builder=new AlertDialog.Builder(RegisterActivity.this);
                                builder.setMessage("회원 등록에 실패했습니다.")
                                        .setNegativeButton("다시 시도", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                Intent intent=new Intent(RegisterActivity.this, RegisterActivity.class);
                                                RegisterActivity.this.startActivity(intent);
                                            }
                                        })
                                        .create()
                                        .show();
                            }

                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                };

                RegisterRequest registerRequest=new RegisterRequest(userID, userPassword,
                        userName,userAge, responseListener);
                RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);

                queue.add(registerRequest);
            }
        });
    }


    // 영문만 허용 (숫자 포함)

    protected InputFilter filter= new InputFilter() {

        public CharSequence filter(CharSequence source, int start, int end,

                                   Spanned dest, int dstart, int dend) {



            Pattern ps = Pattern.compile("^[a-zA-Z0-9]+$");

            if (!ps.matcher(source).matches()) {

                return "";

            }

            return null;

        }

    };

    // 한글만 허용
    public InputFilter filterKor = new InputFilter() {

        @Override
        public CharSequence filter(CharSequence source, int start, int end,

                                   Spanned dest, int dstart, int dend) {



            Pattern ps = Pattern.compile("^[ㄱ-가-힣]+$");

            if (!ps.matcher(source).matches()) {

                return "";

            }

            return null;

        }

    };






}

 

 

class RegisterRequest

package kr.co.dothome.braverokmc.managerment;


import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by choi on 2017-05-02.
 */

public class RegisterRequest extends StringRequest {

    final static private String URL="http://braverokmc2.dothome.co.kr/Register.php";

    private Map<String, String> parameters;

/*    public RegisterRequest(int method, String url,
                           Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
    }*/

    public RegisterRequest(String userID, String userPassword, String userName, int userAge,
                           Response.Listener<String> listener){

        super(Method.POST, URL, listener, null);
        try {
            //한글 깨짐 방지
          //  String st=URLEncoder.encode(userName, "UTF-8");

            parameters=new HashMap<>();
            parameters.put("userID", userID);
            parameters.put("userPassword", userPassword);
            parameters.put("userName", userName);
            parameters.put("userAge", userAge +"");


        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    @Override
    protected Map<String, String> getParams() {
        return parameters;
    }


}



 

 

R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="kr.co.dothome.braverokmc.managerment.MainActivity">

    <TextView
        android:id="@+id/welcome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        />

    <TextView
        android:id="@+id/idTextText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="아이디"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

    <TextView
        android:id="@+id/idText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />


    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="비밀번호"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

    <TextView
        android:id="@+id/pswordText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

    <Button
        android:id="@+id/manageMentButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="회원관리"
        android:layout_gravity="center_horizontal"

        />


</LinearLayout>

 

R.layout.activity_register

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="kr.co.dothome.braverokmc.managerment.RegisterActivity">

    <EditText
        android:id="@+id/idText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="아이디"
        android:inputType="" />

    <EditText
        android:id="@+id/pswordText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="비밀번호"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/nameText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:hint="이름"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/ageText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="나이"
        android:inputType="number" />

    <Button
        android:id="@+id/registerbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:text="회원가입" />



</LinearLayout>

 

 

 

R.layout.login_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="kr.co.dothome.braverokmc.managerment.LoginActivity">

    <EditText
        android:id="@+id/idText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:ems="10"
        android:hint="아이디"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/pswordText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/idText"
        android:ems="10"
        android:hint="비밀번호"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/text1"
        android:layout_marginTop="86dp"
        android:text="로그인" />

    <TextView
        android:id="@+id/registerBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/loginButton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:text="회원가입"
        android:textAppearance="@style/TextAppearance.AppCompat" />
</RelativeLayout>

 

 

16강의 - 핵심 추가및 변경 코드

 

 

 

List.php

 



<?php 

	//http://braverokmc2.dothome.co.kr/Login.php?userID=hong&userPassword=1111
	
	
	/* mysql_connect('localhost', 'mysql_user', 'mysql_password'); */
	$con =mysql_connect("localhost", "braverokmc2", "ehtjrhks5382");
	
	
	/* 	3. Mysql 입출력 인코딩을 지정
	 Mysql 연동을 한다면 입출력 인코딩이 달라서가 원인이 될 수 있다고 합니다.
	아래 3줄의 소스를 추가함으로써 해결하였습니다.
	
	출처: http://jhrun.tistory.com/140 [JHRunning] */
	
	mysql_query("set session character_set_connection=utf8;");
	
	mysql_query("set session character_set_results=utf8;");
	
	mysql_query("set session character_set_client=utf8;");	



	mysql_set_charset("utf8");//sql 에서 한글 깨짐
	$stmt ="select * from braverokmc2.USER ";
	$rs =mysql_query($stmt, $con);
	$response =array();
	
	while($row =mysql_fetch_array($rs)){
		array_push($response, array("userID" =>$row[0], "userPassword"=>$row[1],
			"userName"=>$row[2],  "userAge" =>$row[3] ) );
	}
	
	echo json_encode(array("response" =>$response));
	
	mysql_close($con);
	
	
	
	
	




?>

 

 

class MainActivity

 

package kr.co.dothome.braverokmc.managerment;

import android.content.Intent;
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.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


//로그인 성공시
public class MainActivity extends AppCompatActivity {


    TextView idText, pswordText, welcome;
    Button manageMentButton;
    String userID, userPassword,message;
    ArrayList<MemberDTO> members ;


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

        idText =(TextView)findViewById(R.id.idText);
        pswordText =(TextView)findViewById(R.id.pswordText);
        welcome=(TextView)findViewById(R.id.welcome);

        manageMentButton=(Button)findViewById(R.id.manageMentButton);


        manageMentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new BackgroundTask().execute();
            }
        });


        Intent intent=getIntent();
        if(intent!=null){
            userID=intent.getStringExtra("userID");
            userPassword=intent.getStringExtra("userPassword");
            message ="환영 합니다. " + userID + "님!";

            idText.setText(userID);
            pswordText.setText(userPassword);
            welcome.setText(message);
        }


        if(!userID.equals("admin")){
            manageMentButton.setVisibility(View.GONE);
        }


    }


    //실제 회원 목록 가져오기,, AsyncTask 이용 또는 스레드 사용

    class BackgroundTask extends AsyncTask<Void, Void,  String>{

        String target;

        @Override
        protected void onPreExecute() {
            target="http://braverokmc2.dothome.co.kr/List.php";
        }


        @Override
        protected String doInBackground(Void... params) {

            InputStream is=null;
            InputStreamReader isr =null;
            BufferedReader reader=null;
            StringBuffer stringBuffer =new StringBuffer();


            try{

                URL url =new URL(target);
                HttpURLConnection httpURLConnection =(HttpURLConnection)url.openConnection();

                httpURLConnection.setConnectTimeout(10000);
                if(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_OK){

                    is=httpURLConnection.getInputStream();
                    reader=new BufferedReader(new InputStreamReader(is));

                    while(true){
                        String stringLine =reader.readLine();
                        if(stringLine==null)break;
                        stringBuffer.append(stringLine+"\n");
                    }

                }

                parsing(stringBuffer.toString());
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    if(reader!=null) reader.close();
                    if(isr!=null) isr.close();
                    if(is!=null)is.close();

                }catch (Exception e){
                    e.printStackTrace();
                }

            }
            return null;
        }


        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }


        @Override
        protected void onPostExecute(String s) {

            Intent intent =new Intent(MainActivity.this, ManagementActivity.class);

            Bundle bundle =new Bundle();
            bundle.putSerializable("members", members);
            intent.putExtra("members",  bundle);
            startActivity(intent);

        }

    }


    public void parsing(String data){

        members=new ArrayList<>();
        try{
            JSONObject jsonObject =new JSONObject(data);
            JSONArray jsonArray=new JSONArray(jsonObject.getString("response"));

            //arrayList 클리어
            members.clear();

            for(int i=0; i<jsonArray.length(); i++){

                MemberDTO member=new MemberDTO();

                JSONObject jsonObject1=(JSONObject)jsonArray.get(i);
                member.setUserID(jsonObject1.getString("userID"));
                member.setUserName(jsonObject1.getString("userName"));
                member.setUserPassword(jsonObject1.getString("userPassword"));
                member.setUserAge(jsonObject1.getInt("userAge"));

                members.add(member);
            }


        }catch (Exception e){
            e.printStackTrace();
        }

    }





}

 

R.layout.activity_main  

추가

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="kr.co.dothome.braverokmc.managerment.MainActivity">

    <TextView
        android:id="@+id/welcome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        />

    <TextView
        android:id="@+id/idTextText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="아이디"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

    <TextView
        android:id="@+id/idText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />


    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="비밀번호"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline" />

    <TextView
        android:id="@+id/pswordText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1" />

    <Button
        android:id="@+id/manageMentButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="회원관리"
        android:layout_gravity="center_horizontal"

        />


</LinearLayout>

 

 

class MemberDTO

package kr.co.dothome.braverokmc.managerment;

import java.io.Serializable;

/**
 * Created by choi on 2017-05-08.
 */

public class MemberDTO implements Serializable {

    private static final long serialVersionUID = 1L;

    private String userID;
    private String userPassword;
    private String userName;
    private Integer userAge;


    public MemberDTO() {
    }

    public MemberDTO(String userID, String userPassword, String userName, Integer userAge) {
        this.userID = userID;
        this.userPassword = userPassword;
        this.userName = userName;
        this.userAge = userAge;
    }

    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public void setUserPassword(String userPassword) {
        this.userPassword = userPassword;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getUserAge() {
        return userAge;
    }

    public void setUserAge(Integer userAge) {
        this.userAge = userAge;
    }

    @Override
    public String toString() {
        return "MemberDTO{" +
                "userID='" + userID + '\'' +
                ", userPassword='" + userPassword + '\'' +
                ", userName='" + userName + '\'' +
                ", userAge=" + userAge +
                '}';
    }
}

 

class ManagementActivity

package kr.co.dothome.braverokmc.managerment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;

public class ManagementActivity extends AppCompatActivity {

    //TextView userListTextView;
    ListView listView;


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

      //  userListTextView=(TextView)findViewById(userListTextView);
        listView=(ListView)findViewById(R.id.listView);


        Intent intent=getIntent();
        if(intent!=null) {

            Bundle membersBundle = intent.getBundleExtra("members");
            ArrayList<MemberDTO> membsers = (ArrayList<MemberDTO>) membersBundle.get("members");


            UserListAdapter adapter =new UserListAdapter(ManagementActivity.this, membsers);
            listView.setAdapter(adapter);

/*
            StringBuilder builder = new StringBuilder();
            for (MemberDTO dto : membsers) {

                builder.append("아이디 : "  + dto.getUserID()+"\n");
                builder.append( "이름 : " + dto.getUserName() +"\n");
                builder.append("패스워드 : " + dto.getUserPassword() +"\n");
                builder.append("나이 : " +dto.getUserAge() + "\n");
                builder.append("--------------------------------------\n\n\n");

            }
*/


        }


    }




}







 

R.layout.activity_management

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#d1d1d1">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:layout_marginTop="10dp"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:dividerHeight="10dp"
        android:divider="#d1d1d1">


    </ListView>






</LinearLayout>

 

 

user.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:background="#ffffff"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userID"
        android:text="아이디"
        android:textSize="20dp"
        android:textColor="#00a7f5"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userPassword"
        android:text="비밀번호"
        android:textSize="15dp"
        android:textColor="#f92a3f"
        />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userName"
        android:text="이름"
        android:textSize="26dp"
        android:textColor="#000000"
        />



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userAge"
        android:text="나이"
        android:textSize="18dp"
        android:textColor="#775533"
        />





</LinearLayout>

 

class UserListAdapter

package kr.co.dothome.braverokmc.managerment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by choi on 2017-05-08.
 */

public class UserListAdapter extends BaseAdapter {

    Context mContext;
    List<MemberDTO> memberList;
    LayoutInflater inflater;

    public UserListAdapter(Context mContext, List<MemberDTO> memberList) {
        this.mContext = mContext;
        this.memberList = memberList;
        inflater=(LayoutInflater)mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return memberList.size();
    }

    @Override
    public Object getItem(int position) {
        return memberList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView==null){
            convertView=inflater.inflate(R.layout.user, parent, false);
        }

        TextView userID =(TextView)convertView.findViewById(R.id.userID);
        TextView userName =(TextView)convertView.findViewById(R.id.userName);
        TextView userPassword=(TextView)convertView.findViewById(R.id.userPassword);
        TextView userAge =(TextView)convertView.findViewById(R.id.userAge);


        userID.setText("아이디 : " +memberList.get(position).getUserID());
        userName.setText("이름 : " +memberList.get(position).getUserName());
        userPassword.setText("패스워드 : "+ memberList.get(position).getUserPassword());
        userAge.setText("나이 : " + memberList.get(position).getUserAge());

        if((position%2)==1){
          //  convertView.setBackgroundColor(0xffff8800);

        }else {
            convertView.setBackgroundColor(0xff748790);
        }

        return convertView;
    }




}




 

 

 

 

 

 

 

 

 

 

 

 

 

android

 

about author

PHRASE

Level 60  머나먼나라

서낭에 가서 절만 한다 , 뜻도 모르고 남의 흉내만 열심히 낸다는 말.

댓글 ( 6)

댓글 남기기

작성