我在我的 HTTP 请求中遇到了一些奇怪的行为。我有一些用户说这个调用永远不会回来(标记它是异步调用的微调器永远不会消失)。我以前见过这种情况,但我将其归因于通过 Charles Proxy 的模拟器。直到现在我还没有在实际手机上看到它。
我不确定什么会导致这种情况发生,这就是我在这里发布它的原因。这是调用,使用 Jackson 将结果反序列化为值对象。我看到模拟器冻结的两个地方是 httpclient.execute(httpGet); 和 getObjectMapper().readValue(jp, SyncVO.class);。
在调试时,单步执行有问题的语句会导致调试器永远无法控制单步执行。同时,我看到请求通过 Charles 从服务器发出并返回。只是该应用程序似乎没有得到响应,只是坐在那里。
所以,这里是代码。谢谢你的帮助!
public SyncVO sync(String userId, long lastUpdate, boolean includeFetch) throws IOException {
SyncVO result = null;
String url = BASE_URL + "users/" + userId + "/sync" + "?" + "fetch=" + includeFetch;
if (lastUpdate > 0) {
url += "&updatedSince=" + lastUpdate;
}
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("Accept", "application/json");
httpGet.setHeader("Accept-Encoding", "gzip");
httpGet.setHeader(AUTHORIZATION, BEARER + " " + mOAuthToken);
httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT_STRING);
httpclient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
HttpResponse response = httpclient.execute(httpGet);
if (isUnauthorized(response)) {
APPLICATION.needReauthentication();
return null;
}
if (response != null) {
InputStream stream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
stream = new GZIPInputStream(stream);
}
InputStreamReader inReader = new InputStreamReader(stream, "UTF-8");
JsonParser jp = mJsonFactory.createJsonParser(inReader);
result = getObjectMapper().readValue(jp, SyncVO.class);
}
return result;
}
private ObjectMapper getObjectMapper() {
return (new ObjectMapper()
.configure(Feature.AUTO_DETECT_FIELDS, true)
.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true));
}