0

当您按下电源按钮恢复屏幕时,设备进入“睡眠”或省电模式(屏幕变黑),一切看起来都很好并且功能正常。但是,当我尝试进行任何 HTTP 调用(使用DefaultHttpClient)时,我会遇到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

好的。到了这一点的底部。这是一个错误的身份案例......各种各样的。发生的事情是当前会话被其他移动设备上使用相同帐户登录的人无效。然后服务器使先前的会话无效。一切都很好,但是他们没有返回 401,而是返回了 301,在 HTTPClient 中触发了 ClientProtocolException。多哈。浪费了一天。感谢大家的帮助!

于 2015-05-28T23:13:57.530 回答