서버 연결
Activity -> Request -> queue -> PHP -> server(db) (반대로)
Activity -> Request -> queue
public void onClick(View view) {
String userID = et_id.getText().toString();
String userPassword = et_password.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// 통신이 실패할 수도 있음.
JSONObject jsonObject = new JSONObject(response);
boolean success = jsonObject.getBoolean("success");
if (success) {
// db에 있는 id, password
String userID = jsonObject.getString("userID");
String userPassword = jsonObject.getString("userPassword");
Toast.makeText(getApplicationContext(), "로그인에 성공하였습니다 :D", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("userID", userID);
intent.putExtra("userPassword", userPassword);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "로그인에 실패하였습니다. TT", Toast.LENGTH_SHORT).show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(userID, userPassword, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
// request 파일
public class LoginRequest extends StringRequest {
// 서버 URL 설정 ( PHP 파일 연동)
final static private String Url = "http://wlsgur4011.dothome.co.kr/Login.php";
private Map<String, String> map;
public LoginRequest(String userID, String userPassword, Response.Listener<String> listener) {
super(Method.POST, Url, listener, null); // POST 방식 <-> ㅎㄷㅅ qkdtlr
map = new HashMap<>();
map.put("userID", userID);
map.put("userPassword", userPassword);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return map;
}
}
PHP -> server(db)
<?php
$con = mysqli_connect("localhost", "wlsgur4011", "sw4jh!tfgss", "wlsgur4011");
mysqli_query($con,'SET NAMES utf8');
$userID = $_POST["userID"];
$userPassword = $_POST["userPassword"];
$statement = mysqli_prepare($con, "SELECT * FROM USER WHERE userID = ? AND userPassword = ?");
mysqli_stmt_bind_param($statement, "ss", $userID, $userPassword);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $userPassword, $userName, $userAge);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)) {
$response["success"] = true;
$response["userID"] = $userID;
$response["userPassword"] = $userPassword;
$response["userName"] = $userName;
$response["userAge"] = $userAge;
}
echo json_encode($response);
?>
string을 파일이름으로 변경해서 적용
getResources().getIdentifier(type, "drawable", getActivity().getPackageName());
// ex) "enfj" -> R.drawable.enfj
fragment 형식
public class Fragment2 extends Fragment {
public Fragment2() {
}
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//onCreateView : 그냥 fragment 실행하면 하는거
return inflater.inflate(R.layout.fragment2, container, false);
//fragment의 return 값은 layout을 inflate해서 보내주는 거임. 그리고 이게 표시되는 거!!!!!!
}
}
Intent : 창 옮기기
public class MainActivity extends AppCompatActivity {
private Button btn_move;
@Override
protected void onCreate(Bundle savedInstanceState) { // 앱에서 보여주는 모든 거
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 첫 layout
btn_move = findViewById(R.id.btn_move); // ojbect 찾아줌 ex) 버튼
btn_move.setOnClickListener(new View.OnClickListener() { //click api
@Override
public void onClick(View view) { // click 하면 이거 해줄게
Intent intent = new Intent(MainActivity.this, SubActivity.class); //this: 현재, class: 다른 곳
//효과 정의
startActivity(intent); // 효과실행 - 창바꾸기
}
});
}
}
앱 종료할 때 데이터 남기기 + 남은 데이터 표시하기
@Override
protected void onCreate(Bundle savedInstanceState) {
// 시작할 때 실행하는 함수
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_save = (EditText)findViewById(R.id.et_save);
SharedPreferences sharedPreferences = getSharedPreferences(shared, 0);
// 4
String value = sharedPreferences.getString("hong", "");
// 5 key , default
et_save.setText(value);
// 6. 저장된 값 불러와짐
}
@Override
protected void onDestroy() {
// 1. 앱을 종료 시킬 때(이 페이지를 나갈때) 호출
super.onDestroy();
SharedPreferences sharedPreferences = getSharedPreferences(shared, 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
String value = et_save.getText().toString();
editor.putString("hong", value);
// 2. hong이라는 이름으로 value값을 저장할거야
editor.commit();
// 3. save를 완료!
}
list : 코드의 데이터와 앱의 데이터 연결
list = (ListView)findViewById(R.id.list);
// 1. xml의 list 선언
List<String> data = new ArrayList<>();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, data);
// adapter : 중간다리 역할을 해줌, data와 연결
list.setAdapter(adapter);
// list 에 내가 선언한 adapter 장착!
data.add("홍드로이드");
data.add("안드로이드");
data.add("사과");
// data에는 내가 코드에서 변경할 수 있음
adapter.notifyDataSetChanged();
// 저장할게!
버튼 효과 지정
public class MainActivity extends AppCompatActivity {
EditText et_id; // 글자 창
Button btn_test; // 버튼
@Override
protected void onCreate(Bundle savedInstanceState) {
// 처음실행하는 생명주기
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//xml에 있는 UI들과 연결
et_id = findViewById(R.id.et_id);
btn_test = findViewById(R.id.btn_test);
btn_test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { //click했을 때 action
et_id.setText("홍드로이드"); // text값을 변환시켜줌
}
});
}
}
xml 예시파일
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
LinearLayout : 쉬운거
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<EditText
android:id="@+id/et_id"
@+id : 그냥 붙이는 거, 이 id 값을 java에서 이용함
android:layout_width="300dp"
android:layout_height="wrap_content"
wrap_content : 알아서 지정해줌
android:hint="아이디를 입력하세요"
/>
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="버튼"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<!-- wrap_content : 안에 있는 content만큼
gravity : 정렬
-->
facebook key hash
keytool -exportcert -alias androiddebugkey -keystore "C:\Users\q\.android\debug.keystore" | "C:\openssl\bin\openssl" sha1 -binary | "C:\openssl\bin\openssl" base64