안드로이드는 설계 상으로 메인 쓰레드에서 네트워크를 허용하지 않지만 강제적 일 수는 있지만 좋은 습관은 아닙니다. 따라서 사용자 인증을 위해 데이터베이스에서 응답을받을 수 있도록 주 스레드와 다른 스레드를 동기화하려고합니다. 내 동기는 checkLogin
의 마지막 문으로 thread.join()
를 넣어 다음 synhronization을 모두 제거 .. 안드로이드의 메인 스레드와 다른 스레드간에 동기화
이
주 스레드에서 코드입니다 ..if (loginBTN.getId() == ((Button) v).getId()) {
String emailId = loginField.getText().toString().trim();
String password = passwordField.getText().toString().trim();
postParameters.add(new BasicNameValuePair("username", emailId));
postParameters.add(new BasicNameValuePair("password", password));
try {
System.out.println("b4 response" + responseToString);
checkLogin();
System.out.println("after response" + responseToString);
synchronized (this) {
while (isAvailable == false) {
wait();
}
if (responseToString.equalsIgnoreCase("1")) {
statusLBL.setText("Login Successful...");
}
else {
statusLBL.setText("Sorry Incorrect Username or Password...");
}
}
}
catch (Exception e) {
statusLBL.setText(e.toString());
}
}
private void checkLogin() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient http = new DefaultHttpClient();
HttpPost request = new HttpPost("http://example.org/api/android_gradhub/login_user.php");
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
HttpResponse response = http.execute(request);
HttpEntity entity = response.getEntity();
// responseToString = EntityUtils.toString(entity);
synchronized (this) {
while (isAvailable == true) {
wait();
}
responseToString = EntityUtils.toString(entity);
notifyAll();
}
System.out.println("response " + responseToString);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
무엇이 문제입니까? –
무엇이 당신의 질문입니까? – Daniel
동기화가 작동하지 않습니다 .. – user2421377