LoginActivity.java
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class LoginActivity extends AppCompatActivity {
Button btn_login, btn_new;
EditText tvId, tvPw;
RequestQueue queue;
StringRequest request;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_login = findViewById(R.id.btn_login);
btn_new = findViewById(R.id.btn_new);
tvId = findViewById(R.id.tvId);
tvPw = findViewById(R.id.tvPw);
queue = Volley.newRequestQueue(LoginActivity.this);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int method = Request.Method.POST;
String server_url = "http://172.30.1.36:8085/android/Login";
request = new StringRequest(
method,
server_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(LoginActivity.this,
"로그인 성공"+response,
Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this,
"로그인 실패"+error.toString(),
Toast.LENGTH_SHORT).show();
}
}
){
@Nullable
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param = new HashMap<>();
param.put("id",tvId.getText().toString());
param.put("pw",tvPw.getText().toString());
return param;
}
};
queue.add(request);
}
});
btn_new.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this,JoinActivity.class);
startActivity(intent);
}
});
}
}
저번 정리한 글을 참고하면 좋다.
int method = Request.Method.POST;
String server_url = "http://172.30.1.36:8085/android/Login";
POST 방식으로 보내기 위해 Method를 설정한다.
server_url은 뒤에 /Login의 위치에 어떤 값이 들어오냐에 따라 받는 곳이 달라진다.
{
@Nullable
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param = new HashMap<>();
param.put("id",tvId.getText().toString());
param.put("pw",tvPw.getText().toString());
return param;
}
}
param.put("키", 값) : 데이터를 보내는데 키값을 정하여 데이터 값을 보낸다.
activity_main.xml
JoinActivity
package com.example.ex0421;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class JoinActivity extends AppCompatActivity {
Button reg_join;
EditText regId, regPw, regNick;
RequestQueue queue;
StringRequest request;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_join);
reg_join = findViewById(R.id.join_join);
regId = findViewById(R.id.join_id);
regNick = findViewById(R.id.join_nick);
regPw = findViewById(R.id.join_pw);
queue = Volley.newRequestQueue(JoinActivity.this);
reg_join.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int method = Request.Method.POST;
String server_url ="http://172.30.1.36:8085/android/Join";
request = new StringRequest(
method,
server_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(JoinActivity.this,
"요청성공",
Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(JoinActivity.this,
"요청실패"+error.toString(),
Toast.LENGTH_SHORT).show();
}
}
){
//POST 방식으로 데이터 전송할 때 사용하는 메소드
@Nullable
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> param = new HashMap<>();
param.put("id",regId.getText().toString());
param.put("pw",regPw.getText().toString());
param.put("nick",regNick.getText().toString());
return param;
}
};
queue.add(request);
}
});
}
}
Map : Key와 value 형태(타입 변경 가능)로 데이터를 저장하는 자료구조
activity_join.xml