0

電源ボタンを押して画面を復元すると、デバイスが「スリープ」または省電力モード (画面が黒くなる) になると、すべてが正常に機能します。DefaultHttpClientただし、(を使用して) HTTP 呼び出しを実行しようとすると、ClientProtocolException例外が発生します。その後、アプリケーションを終了して再び戻る必要があり、すべて正常に動作します。新しい URLConnection パッケージが利用可能であることは知っていますが、リファクタリングにはしばらく時間がかかります。これを修正する方法はありますか?

失敗するコードのサンプルを次に示しますが、デバイスがスリープするまで問題なく動作します。

public JSONObject mediaAmazonUploadAuthParams(String key, String contentType, Boolean publicRead) throws MyException {
    JSONObject authParams = null;
    HttpClient httpClient = new DefaultHttpClient();
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    HttpPost postMethod = new HttpPost(getBaseUrl() + "myapi");  
    postMethod.setHeader("SessionToken", mSessionToken);
    int versionCode = Utilities.getVersionCode(GlobalState.getInstance().context);
    String versionName = "0.0.0";
    String clientHeader = "android-0.0.0"; 
    postMethod.setHeader("Client", clientHeader);
    postMethod.setHeader( "Content-Type", "application/json");

    GatekeeperResult result;
    JSONObject postJson = new JSONObject();
    try {
        postJson.put("key", key);
        postJson.put("contentType", contentType);
        postJson.put("publicRead", publicRead);
        String postString = postJson.toString();
        postMethod.setEntity(new StringEntity(postString));
        String response = httpClient.execute(postMethod, responseHandler);
        result = new Result(new JSONObject(response));
    } catch (Exception e) {
        Log.e("Client","mediaAmazonUploadAuthParams(), Error httpClient execute ",e);
        throw new MyException(e);
    }

これは、失敗時に取得するスタック トレースです。

05-28 11:36:20.891: E/GatekeeperApiClient(8999): org.apache.http.client.ClientProtocolException
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:589)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:685)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:659)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:648)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at com.invocore.fastfield.gatekeeper.GatekeeperApiClient.mediaAmazonUploadAuthParams(GatekeeperApiClient.java:906)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at com.invocore.fastfield.utility.LibraryUtils.saveFormMedia(LibraryUtils.java:156)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at com.invocore.fastfield.FormActivity$submitFormResultTask.doInBackground(FormActivity.java:2562)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at com.invocore.fastfield.FormActivity$submitFormResultTask.doInBackground(FormActivity.java:1)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at java.lang.Thread.run(Thread.java:838)
05-28 11:36:20.891: E/GatekeeperApiClient(8999): Caused by: org.apache.http.ProtocolException: Received redirect response HTTP/1.1 301 Moved Permanently but no location header
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at org.apache.http.impl.client.DefaultRedirectHandler.getLocationURI(DefaultRedirectHandler.java:103)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:944)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:491)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:587)
05-28 11:36:20.891: E/GatekeeperApiClient(8999):    ... 13 more

注: これは、画面がスリープ状態になるだけでは発生しません...これも時間の要因です (10 分以上アイドル状態)。怠惰のために何かが記憶から引き出されることに関連しているのではないかと思いますか? 前もって感謝します!

4

1 に答える 1

0

Ok。これの底に着きました。これは身元の間違いのケースでした... ある種の。何が起こっていたかというと、別のモバイル デバイスで同じアカウントを使用してログインしている誰かによって、現在のセッションが無効にされていたということです。その後、サーバーは前のセッションを無効にしました。すべて問題ありませんが、401 を返す代わりに 301 を返し、HTTPClient で ClientProtocolException をトリガーしました。ドー。無駄な一日。みんな助けてくれてありがとう!

于 2015-05-28T23:13:57.530 に答える