2012-12-03 60 views
0

基本上,我的想法是檢查網絡會話是否仍然有效,如果沒有,我開始主動活動,自動記錄用戶。從新線程中的類開始活動

我有這個工作,不知道這是否是最好的方法。如果有人有更好的方法,請讓我知道。

感謝

用途:

@Override 
    public void onResume() { 
     super.onResume(); 
     new LoginCheck(this,new Intent(this,MyActivity.class)); 

    } 

的Class

public class LoginCheck extends Application { 


Intent home; 
Activity activity; 

public LoginCheck(Activity activity, Intent home) { 
    this.activity = activity; 
    this.home = home; 
    new Check().execute(); 
} 

public class Check extends AsyncTask { 

    @Override 
    protected Object doInBackground(Object... objects) { 
     try { 
      InputStream is = null; 
      String result = ""; 
      JSONObject jArray = null; 
      PersistentCookieStore myCookieStore = new PersistentCookieStore(MyApp.getAppContext()); 
      //http post 
      DefaultHttpClient mClient = AppSettings.getClient(); 
      try { 
       HttpPost request = new HttpPost(MyApp.getServiceUrl() + "/Api/Login/AmILoggedIn"); 
       mClient.setCookieStore(myCookieStore); 
       HttpResponse response = mClient.execute(request); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
      } catch (Exception e) { 
       Log.e("log_tag", "Error in http connection " + e.toString()); 
      } 

      //convert response to string 
      try { 

       BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       result = sb.toString(); 
      } catch (Exception e) { 
       Log.e("log_tag", "Error converting result " + e.toString()); 
      } 
      final String r = result; 
      final Intent i = home; 
      activity.runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 

        try { 

         JSONObject j = new JSONObject(r); 
         if (!j.getBoolean("Success")) { 
          try { 
           activity.startActivity(i); 
          } catch (Exception e) { 
           Log.e("log_tag", e.getMessage()); 
          } 
         } 


        } catch (JSONException e) { 

         Log.e("log_tag", "Error parsing data " + e.toString()); 
        } 
       } 
      }); 

      return true; 

     } catch (Exception e) { 

     } 

     return null; 
    } 
} 

}

+0

我不是你在做什麼完全清楚,但你可以移動代碼中的run()''到onPostExecute()'。這種方法已經可以訪問UI線程... – Sam

+0

良好的捕獲,這將清理它一點。 – user1723341

回答

0

這裏是清理版本

用途:

new LoginCheck() { 
     @Override 
     public void LoggedIn() { 
      //To change body of implemented methods use File | Settings | File Templates. 
     } 

     @Override 
     public void LoggedOut() { 
      //To change body of implemented methods use File | Settings | File Templates. 
     } 
    }; 

public abstract class LoginCheck { 
public LoginCheck(){ 
    new Check().execute(true); 
} 
public class Check extends AsyncTask<Boolean,Boolean,Boolean>{ 
@Override 
protected Boolean doInBackground(Boolean... objects) { 

    JSONStringer jsonSend = null; 

    try { 
     jsonSend = new JSONStringer() 
       .object() 
       .endObject(); 
    } catch (JSONException e) { 
     Log.e("log_tag", "Error creating Json " + e.toString()); 
    } 

    JSONObject result = JsonPost.postJSONtoURL(MyApp.getServiceUrl() + "/Api/Login/AmILoggedIn", jsonSend); 

    try { 
     if (result.getBoolean("Success")) { 
      return true; 
     } 
    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data " + e.toString()); 
    } 
    try { 
     if (!result.getBoolean("Success")) { 
      return false; 
     } 
    } catch (JSONException e) { 
     Log.e("log_tag", "Error parsing data " + e.toString()); 
    } 

    return false; 
} 



@Override 
protected void onPostExecute(Boolean result){ 
    if(result) 
     LoggedIn(); 
    if(!result) 
     LoggedOut(); 

} 
    } 




public abstract void LoggedIn(); 
public abstract void LoggedOut(); 

}