chrometophone (7)

実は今から出掛けるんですが、ちょっとした間隙を縫ってエントリ投入してみる。
今確認してるのは AppEngineClient です。これ、他に流用できる位完成度が高いと見てます。

makeRequest メソド

ええと手続き定義が以下。

    public HttpResponse makeRequest(String urlPath, List<NameValuePair> params) throws Exception {
        HttpResponse res = makeRequestNoRetry(urlPath, params, false);
        if (res.getStatusLine().getStatusCode() == 500) {
            res = makeRequestNoRetry(urlPath, params, true);
        }
        return res;
    }

まず makeRequestNoRetry の第三引数を false にして呼び出しといて、レスポンスが 500 だった場合、第三引数を true にして呼び出してます。500 は Internal Server Error って事で、第三引数の意味合いがカギになってくるんかな。

makeRequestNoRetry メソド

ええと、第三引数にフォーカスを絞って最初の部分のみ見てみます。

    private HttpResponse makeRequestNoRetry(String urlPath, List<NameValuePair> params, boolean newToken)
            throws Exception {

        // Get auth token for account
        Account account = new Account(mAccountName, "com.google");
        String authToken = getAuthToken(mContext, account);
        if (authToken == null) throw new PendingAuthException(mAccountName);
        if (newToken) {  // invalidate the cached token
            AccountManager accountManager = AccountManager.get(mContext);
            accountManager.invalidateAuthToken(account.type, authToken);
            authToken = getAuthToken(mContext, account);
        }

500 が戻ってくるケイスを認証失敗に限定してます。newToken が true の場合、AccountManager#invalidateAuthToken を呼び出して getAuthToken してます。なんで invalidateAuthToken なのか、という部分については AccountManager | Android Developer に以下な記述があります。

Important: If the request fails with an authentication error, it could be that a cached auth token is stale and no longer honored by the server. The application must call invalidateAuthToken(String, String) to remove the token from the cache, otherwise requests will continue failing! After invalidating the auth token, immediately go back to the "Request an auth token" step above. If the process fails the second time, then it can be treated as a "genuine" authentication failure and the user notified or other appropriate actions taken.

もっかいヤッて駄目なら認証以外の原因だろうね的ソレ。以降はちょっと確認に時間がかかりそげなので別途とゆー事にさせて下さひ。