안드로이드 onResponse
조회수 493회
login_button = findViewById( R.id.login_button );
login_button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("로그인버튼 눌림");
String UserNumber = login_number.getText().toString();
String UserPwd = login_password.getText().toString();
System.out.println(UserNumber+UserPwd+"학번이랑 패스워드는 들어감");
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println("onResponse");
try {
System.out.println(response+"로그인 값 받아옴");
JSONObject jsonObject = new JSONObject(response);
boolean success = jsonObject.getBoolean( "success" );
if(success) {//로그인 성공시
String UserNumber = jsonObject.getString( "UserNumber" );
String UserPwd = jsonObject.getString( "UserPwd" );
String UserName = jsonObject.getString( "UserName" );
Toast.makeText( getApplicationContext(), String.format("%s님 환영합니다.", UserName), Toast.LENGTH_SHORT ).show();
Intent intent = new Intent( LoginActivity.this, VoteListActivity.class );
intent.putExtra( "UserNumber", UserNumber );
intent.putExtra( "UserPwd", UserPwd );
intent.putExtra( "UserName", UserName );
startActivity( intent );
} else {//로그인 실패시
Toast.makeText( getApplicationContext(), "로그인에 실패하셨습니다.", Toast.LENGTH_SHORT ).show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest( UserNumber, UserPwd, responseListener );
RequestQueue queue = Volley.newRequestQueue( LoginActivity.this );
queue.add( loginRequest );
}
});
}
package com.example.myapplication.Activity;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class LoginRequest extends StringRequest {
//서버 URL 설정(php 파일 연동)
final static private String URL = "http://voting.dothome.co.kr/Login.php";
private Map<String, String> map;
public LoginRequest(String UserNumber, String UserPwd, Response.Listener<String> listener) {
super(Method.POST, URL, listener, null);
map = new HashMap<>();
map.put("UserNumber", UserNumber);
map.put("UserPwd", UserPwd);
System.out.println(map+"1");
}
@Override
protected Map<String, String>getParams() throws AuthFailureError {
System.out.println(map+"2");
return map;
}
}
로그인 버튼 (login_button)눌리면 onResponse가 실행이 안되는데 오류도 안생기고 데이터도 잘 넘어가는데 무슨 오류인지도 모르겠고 한번만 도와주세요...!
댓글 입력