2017-09-27 77 views
1

我希望用戶狀態應該離線,一旦他從應用程序註銷。 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; 
    } 
} 
+2

發佈您的UserLoginTask類 – akhilesh0707

回答

0

既然是你需要等待任務,以顯示AlertDialog響應異步任務。

我的意見:在您從註銷過程中獲得HTTP 200 OK響應後,在UserLoginTask()內移動警報對話框。

+0

我試過你的建議,但沒有奏效。同樣的問題,狀態沒有改變 –

0

你應該在alert對話框onclick中調用這個AsyncTask,將execute()更改爲.executeOnExecutor()。然後檢查你的onxreexecute和doInBackground是否工作。

+0

更改更改execute()爲.executeOnExecutor() - 在哪裏? –

+0

mAuthTask = new UserLoginTask(); mAuthTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); – Anilkumar

+0

試過了,但還是不行...... –

0

在對話框內單擊的正向按鈕上調用此方法。也刪除此代碼「(Void)null」作爲byDefault Void本身在asyntask中傳遞。

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(); 
        status = "Offline"; 
        mAuthTask = new UserLoginTask(); 
        mAuthTask.execute(); 
       } 
      }).setNegativeButton("No", null).show();