我的HTTP请求中遇到了一些奇怪的行为。我有一些用户说这个调用不会回来(微调者称它是异步调用永远不会消失)。我之前看到过这种情况,但我把它归功于通过Charles Proxy的模拟器。直到现在,我还没有在实际电话中看到它。Android HTTP客户端死机
我不确定这会发生什么,这就是为什么我在这里发布它。接下来是调用,使用Jackson将结果反序列化为一个Value Object。看到模拟器冻结的两个点是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));
}
我们通常在初始化DefaultHttpClient时指定超时,以避免应用程序永远等待服务器响应。 – yorkw 2012-04-17 22:12:40
我编辑了我的帖子,以澄清我实际上看到请求出去并回来,因为查尔斯,但该应用程序不会做一件事! – Chewie 2012-04-18 00:22:48
是否有任何单例类被调用,为它设置的监听器也是从其他地方调用的?或者是你的微调在某些回调函数中被解雇了? – Shubhayu 2012-04-18 01:32:53