나는 사용자를 확인할 수 있도록 로그인 기능을 만들려고합니다. 나는 AsyncTask 클래스에 Username, Password 변수를 전달하지만, 그것을 사용하기 위해 결과를 얻기 위해 뜨겁다. 어떤 도움이 필요합니까?안드로이드 로그인 HTTP 게시물, 결과를 얻으십시오
btnLogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(txtUsername.getText().toString().trim().length() > 0 && txtPassword.getText().toString().trim().length() > 0)
{
// Retrieve the text entered from the EditText
String Username = txtUsername.getText().toString();
String Password = txtPassword.getText().toString();
/*Toast.makeText(MainActivity.this,
Username +" + " + Password+" \n Ready for step to post data", Toast.LENGTH_LONG).show();*/
String[] params = {Username, Password};
// we are going to use asynctask to prevent network on main thread exception
new PostDataAsyncTask().execute(params);
// Redirect to dashboard/home screen.
login.dismiss();
}
else
{
Toast.makeText(MainActivity.this,
"Please enter Username and Password", Toast.LENGTH_LONG).show();
}
}
});
그런 다음 내가 검사를 수행 할 AsynkTask를 사용하지만 결과를 얻을 수와 변수에 저장하는 방법을 모른다 (I 때문에 웹 사이트의 제한으로 소스 코드의 일부를 게시하고있다). 어떤 도움이 필요합니까?
public class PostDataAsyncTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
// do stuff before posting data
}
@Override
protected String doInBackground(String... params) {
try {
// url where the data will be posted
String postReceiverUrl = "http://server.com/Json/login.php";
Log.v(TAG, "postURL: " + postReceiverUrl);
String line = null;
String fail = "notok";
// HttpClient
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);
// add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("UserName", params[0]));
nameValuePairs.add(new BasicNameValuePair("Password", params[1]));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
line = resEntity.toString();
Log.v(TAG, "Testing response: " + line);
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
Intent Hotels_btn_pressed = new Intent(MainActivity.this, Hotels.class);
startActivity(Hotels_btn_pressed);
// you can add an if statement here and do other actions based on the response
Toast.makeText(MainActivity.this,
"Error! User does not exist", Toast.LENGTH_LONG).show();
}else{
finish();
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
// do stuff after posting data
}
}
[AsyncTask Android 예제] (http://stackoverflow.com/questions/9671546/asynctask-android-example) –
어디에서 응답을 사용할 수 있습니까? 'PostDataAsyncTask'가 별도의 클래스 인 경우에는 onPostExecute가 호출 될 때 Activity에서 실행되는 사용자 정의 리스너를 사용합니다. –
다음 응답에 전달하기 위해 변수에 저장된 응답을 받고 싶습니다. –