当您按下电源按钮恢复屏幕时,当设备进入“睡眠”或省电模式(屏幕变黑)时,所有设备都能正常运行。但是,当我尝试进行任何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分钟)。认为它可能与由于闲置而被拉入内存的东西有关? 提前谢谢!
答案 0 :(得分:0)
确定。到了这个底部。这是一个错误的身份......的种类。发生的事情是当前会话被在不同移动设备上使用同一帐户登录的人无效。然后,服务器使先前会话无效。一切都很好,但是他们没有返回401,而是返回301,在HTTPClient中触发ClientProtocolException。卫生署。浪费了一天。感谢大家的帮助!