2011-08-19 7 views
1

少し重複して申し訳ありませんが、here,hereですが、返信の最後のバイトを読んで閉じると入力ストリーム。それは別の要求の準備ができています。しかし、私がここに何を持っているかを見て、私はいつも新しいリクエストを実行するときにこのエラーが発生します。HttpClient 4.1.xの接続を1つの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; 
    } 

を割り当てられますか?ストリームは読み込まれ、閉じられます。しかし、detectGooglePost(text)メソッドが再び呼び出されるとき(シングルトンの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

関連する問題