0

基本上我的想法是我检查网络会话是否仍然有效,如果不是,我启动自动登录用户的主要活动。

我有这个工作,不确定这是否是最好的方法。如果有人有更好的方法请告诉我。

谢谢

用途:

    @Override
    public void onResume() {
         super.onResume();
         new LoginCheck(this,new Intent(this,MyActivity.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;
    }
}

}

4

1 回答 1

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();

 }
于 2012-12-04T17:11:11.787 回答