我希望用戶狀態應該離線,一旦他從應用程序註銷。 Sp我在註銷菜單中寫了以下語句 -異步功能無法正常工作Android
else if (id == R.id.nav_lout) {
status = "Offline";
mAuthTask = new UserLoginTask();
mAuthTask.execute((Void) null);
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("MyApp")
.setMessage("Are you sure?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
session.logoutUser();
}
}).setNegativeButton("No", null).show();
}
現在問題是Alert Box顯示並且用戶也被註銷。但是他的狀態在數據庫中仍然處於ONLINE狀態。
status = "Offline";
mAuthTask = new UserLoginTask();
mAuthTask.execute((Void) null);
該部分未執行。請幫助。
代碼UserLoginTask分類 -
public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {
UserLoginTask() {
}
@Override
protected Boolean doInBackground(Void... params) {
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", uEmailID));
nameValuePairs.add(new BasicNameValuePair("status", status));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://mywebsite.com/Android/checkIsOnline.php");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString().trim();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
@Override
protected void onPostExecute(final Boolean success) {
mAuthTask = null;
}
@Override
protected void onCancelled() {
mAuthTask = null;
}
}
發佈您的UserLoginTask類 – akhilesh0707