2013-04-19 1 views
2
private HttpResponse doResponse(String url) { 
    HttpClient httpclient = new DefaultHttpClient(getHttpParams()); 
    HttpResponse response = null; 

    try { 
     HttpGet httpget = new HttpGet(url); 
     response = httpclient.execute(httpget); 
    } catch (Exception e) { 
     Log.e(TAG, e.getLocalizedMessage(), e); 
    } 

    return response; 
} 

@Override 
protected String doInBackground(String... urls) { 
    // TODO: attempt authentication against a network service. 
    String url = urls[0]; 
    String result =""; 

    try { 
     HttpResponse response=doResponse(url); 
     result = inputStreamToString(response.getEntity().getContent()); 
     //throws IllegalStateException: Content has been consumed 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     Log.e(TAG, e.getLocalizedMessage(), e); 
    } 

    return result; 
} 

라인IllegalStateException이 : 콘텐츠가 소비되었다 (제 1의 getContent()를)

result = inputStreamToString(response.getEntity().getContent()); 

는 내가 전에 컨텐츠를하지 않았다하더라도

IllegalStateException: Content has been consumed 

가 발생합니다.

나는 내 코드의 마녀 부분은 안드로이드 4.1.1

+2

우리에게 inputStreamToString()'당신의 도움의 도움을 – jnthnjns

+0

@Asok 들으 (나는 이유를 설명 할 수 없다)하지만 예외가 단지 디버그 모드로 여기 '에 대한 코드를 보여주십시오, 아무것도, 실행 모드에 잘못 없다 은 http 요청의 타임 아웃이 될 수있다. – Simo

+1

'getContent()'만 한번 호출했는지 확인하고,'inputStreamToString()'메서드 내에서 호출하지 않도록하라. – jnthnjns

답변

1

당신이 있는지 확인 실행 삼성 갤럭시 탭 2
에 난

response.getEntity().getContent(); 

전에 컨텐츠를 소비하는 알고 싶습니다 getContent() 번만 호출하십시오. inputStreamToString() 메소드에서 호출하지 않는지 확인하십시오. getContent()은 입력 스트림을 반환하고 연결 당 한 번만 입력 스트림을 반환 할 수 있습니다.

Documentation

:

엔티티 새로운 InputStream 객체를 생성()

의 getContent. 동일한 InputStream 객체를 두 번 이상 반환하는 것은 프로그래밍 오류입니다.. 반복 할 수없는 엔티티는이 메소드가 여러 번 호출되는 경우 예외를 throw합니다.