0

基本的には、Webセッションがまだ有効かどうかを確認し、有効でない場合は、ユーザーを自動的にログインさせるメインアクティビティを開始するという考え方です。

私はこれを機能させていますが、それが最善の方法かどうかはわかりません。誰かがより良い方法を持っているなら、私に知らせてください。

ありがとう

使用法:

    @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 に答える