2011-08-19 1 views
1

약간의 중복, here, here에 대해 유감스럽게 생각하지만 응답 본문의 마지막 바이트를 읽고 입력 스트림. 다른 요청을 할 준비가되었습니다. 그러나 내가 여기에 무엇을 가지고 있는지, 그리고 나는 항상 새로운 요청을 실행할 때이 오류를 얻는다.하나의 HttpClient 인스턴스와의 순차적 실행을 위해 HttpClient 4.1.x의 연결 해제

잘못된 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; 
    } 

를 할당? 스트림을 읽고 닫습니다. 하지만 여전히 detectGooglePost (텍스트) 메소드가 다시 호출되면 (싱글 톤 HttpClient 인스턴스), 그 에러를 던집니다. 이 일을해야

답변

1

사람이 시도 :

HttpResponse response; 
    String responseBody; 
    try { 
     response = httpclient.execute(request); 
     int sc = response.getStatusLine().getStatusCode(); 
     if (sc != HttpStatus.SC_OK) 
      throw new SystemException("google status code : " + sc); 

     HttpEntity entity = response.getEntity(); 
     if (entity == null) 
      throw new SystemException("response entity null"); 
     responseBody = EntityUtils.toString(entity); 
    } catch (Exception e) { 
     request.abort(); 
     throw new SystemException("Problem when executing Google request", e); 
    } 
+0

작품 네, 감사합니다 – lisak