gpt4 book ai didi

Android HTTP 客户端卡住

转载 作者:搜寻专家 更新时间:2023-11-01 07:36:39 25 4
gpt4 key购买 nike

我在 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));
}

最佳答案

不要忘记在每次请求后消费实体内容。

HttpEntity entity = response.getEntity();
try {
if (entity != null)
entity.consumeContent();
} catch (IOException e) {
e.printStackTrace();
}

关于Android HTTP 客户端卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10199767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com