1
對不起有點重複,here,here,但他們說,如果你讀了響應主體的最後一個字節並關閉輸入流。它已準備好接受其他請求。但看看我在這裏得到了什麼,而且在執行新的請求時我總是遇到這個錯誤。釋放HttpClient 4.1.x的連接用於一個HttpClient實例的順序執行
無效使用SingleClientConnManager的:連接仍然分配
public String detectGooglePost(String text) throws SystemException {
List<NameValuePair> qParams = new ArrayList<NameValuePair>();
qParams.add(new BasicNameValuePair("key", key));
List<NameValuePair> bodyParams = new ArrayList<NameValuePair>();
bodyParams.add(new BasicNameValuePair("q", text));
HttpPost httpPost = new HttpPost(createURI(qParams));
httpPost.addHeader("X-HTTP-Method-Override", "GET");
try {
httpPost.setEntity(new UrlEncodedFormEntity(bodyParams));
} catch (UnsupportedEncodingException e) {
throw new SystemException("Problem when buidling Google request entity", e);
}
String language = getLanguage(httpPost);
return language;
}
private String getLanguage(HttpUriRequest request) throws SystemException {
HttpResponse response;
try {
response = httpclient.execute(request);
} catch (Exception e) {
throw new SystemException("Problem when executing Google request", e);
}
int sc = response.getStatusLine().getStatusCode();
if (sc != HttpStatus.SC_OK)
throw new SystemException("google status code : " + sc);
StringBuilder builder = getBuilder(response);
String language = processJson(builder.toString());
return language;
}
private StringBuilder getBuilder(HttpResponse response) throws SystemException {
HttpEntity entity = response.getEntity();
if (entity == null)
throw new SystemException("response entity null");
StringBuilder builder = new StringBuilder();
BufferedReader in = null;
String str;
try {
in = new BufferedReader(new InputStreamReader(entity.getContent()));
while ((str = in.readLine()) != null)
builder.append(str);
} catch (IOException e) {
throw new SystemException("Reading input stream of http google response entity problem", e);
} finally {
IOUtils.closeQuietly(in);
}
if (builder.length() == 0)
throw new SystemException("content stream of response entity empty has zero length");
return builder;
}
我應該如何釋放連接?該流被讀取並關閉。但仍然檢測到GooglePost(文本)方法時再次調用(單例HttpClient實例),它會引發該錯誤。
耶的作品,謝謝 – lisak