title: How to implement AsyncTask for twitter.getAccountSettings(): Android
body: <p>I am using twitter4j in my application. I am using below code to check if auth token expired or user not logged in yet</p>
  
  <pre><code>public static boolean isAuthenticated(Context context) {
  
          TwitterPrefrences twitterPref = new TwitterPrefrences(context);     
          String token = twitterPref.getTwitterAuthToken();
          String secret = twitterPref.getTwitterAuthTokenSecrate();
  
          AccessToken a = new AccessToken(token,secret);
          Twitter twitter = new TwitterFactory().getInstance();
          twitter.setOAuthConsumer(Constants.TWITT_CONSUMER_KEY, Constants.TWITT_CONSUMER_SECRET);
          twitter.setOAuthAccessToken(a);
  
          try {
              twitter.getAccountSettings();
              return true;
          } catch (TwitterException e) {
              return false;
          }
  
      }
  </code></pre>
  
  <p>But I am getting following error</p>
  
  <pre><code> 05-02 19:52:23.531: E/AndroidRuntime(1441): FATAL EXCEPTION: main
      05-02 19:52:23.531: E/AndroidRuntime(1441): android.os.NetworkOnMainThreadException
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:71)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpConnection.&lt;init&gt;(HttpConnection.java:50)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at twitter4j.internal.http.HttpResponseImpl.&lt;init&gt;(HttpResponseImpl.java:45)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:178)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:75)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:103)
      05-02 19:52:23.531: E/AndroidRuntime(1441):     at 
  
  twitter4j.Twitter.getAccountSettings(Twitter.java:1440)
  </code></pre>
  
  <p>Caller code as</p>
  
  <pre><code>chbTwOption.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  
              public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                  if(isChecked) {
                      boolean showTwlogin = false;
                      if ("No".equalsIgnoreCase(twitPref.getTwitterAuthToken())
                              || "No".equalsIgnoreCase(twitPref.getTwitterAuthTokenSecrate())) {
                          showTwlogin = true;
                      } else {                        
                          showTwlogin = TwitterUtils.isAuthenticated(AddSchedule.this);
                      }   
  
                      if(showTwlogin) {
                          showSocialMediaLoginMessage(Constants.TW_LOGIN_MESSAGE);
                      }
                  }
              }
          });
  </code></pre>
  
  <p>Its clear that I have to implement <code>AsyncTask</code> for <code>twitter.getAccountSettings()</code>  but how can I get result as a method from <code>AsyncTask</code>? Is there any other way to check auth token expire? Or how can I implement <code>AsyncTask</code> as a method which will return result as <code>isAuthenticated()</code> as I used. </p>
  
  <p>Please guide me. </p>
  
  <p>Thank you.</p>