2011-07-05 4 views
2

나는 아주 이상한 문제에 부딪 쳤고, 나는 어디에서 시작해야하는지 조금이라도 생각하지 못했다.HTTP 응답이 중단 된 문자열은 무엇입니까? - 자바, 플랫폼 안드로이드 3.0

서버에 http 요청을 보내고 응답으로 간단한 문자열을 가져옵니다. 이것은 내 스마트 폰 응용 프로그램에서 정상적으로 작동했습니다. 그것은 심지어 내 브라우저에서 잘 작동합니다. 그러나 스마트 폰 코드를 복사하여 붙여 넣기 만한다고 생각하는 동안 앱의 내 태블릿 (Android 3.0.1) 버전에서는 더 이상 작동하지 않습니다.

나는 디버거를 검사했고 이전 버전은 길이가 2958자인 문자열을 얻습니다. 새로운 버전은 길이가 1334 인 문자열 만 가져옵니다. 새 버전의 URL을 기록한 다음 브라우저에 넣고 2985 자의 문자열을 다시 가져 왔습니다.

내 코드에서 큰 차이점을 찾을 수 없습니다 (아래 참조). 또한, 나는 안드로이드에서 문자열 길이를 제한하는 몇 가지 변화가 있었다고 믿을 수 없습니까?!

아무도 아이디어가 있습니까?


기존 스마트 폰 코드 : 새로운 태블릿 버전

if (CheckInternet()) 
{ 
    myURL = new URL(params[0]); 

    httpClient = AndroidHttpClient.newInstance("android"); 

    if (rtype == RequestType.GET) 
    { 
     httpRequest = new HttpGet(myURL.toExternalForm()); 
    } 
    else 
    { 
     httpRequest = new HttpPost(myURL.toExternalForm()); 

     HttpEntity myEntity = new StringEntity(message, "UTF-8"); 
     ((HttpPost) httpRequest).setEntity(myEntity); 
    } 


    HttpParams httpParams = new BasicHttpParams(); 
    HttpProtocolParams.setHttpElementCharset(httpParams, "UTF-8"); 
    HttpProtocolParams.setContentCharset(httpParams, "UTF-8"); 
    HttpConnectionParams.setConnectionTimeout(httpParams, timeout); 
    HttpConnectionParams.setSoTimeout(httpParams, timeout); 
    httpRequest.setParams(httpParams); 

    response = httpClient.execute(httpRequest); 

    final int statusCode = response.getStatusLine().getStatusCode(); 

    if (statusCode == 300 || statusCode >= 305) 
    { 
     errorMessage = getStatusCodeMessage(statusCode, act); 
    } 
    else 
    { 
     HttpEntity entity = response.getEntity(); 

     if (entity != null) 
     { 

      InputStream instream = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8")); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 
      while ((line = reader.readLine()) != null) 
       sb.append(line); 

      result = sb.toString(); 

     } 
    } 
} 

코드 :

if (CheckInternet()) 
{ 
    if (isCancelled()) return null; //that's for an AsyncTask 

    URL myURL = new URL(params[0]); 
    httpClient = AndroidHttpClient.newInstance("android"); 

    if (isCancelled()) return null; 

    if (params[1] == null) 
    { 
     httpRequest = new HttpGet(myURL.toExternalForm()); 
    } 
    else 
    { 
     httpRequest = new HttpPost(myURL.toExternalForm()); 

     HttpEntity myEntity = new StringEntity(params[1], "UTF-8"); 
     ((HttpPost) httpRequest).setEntity(myEntity); 
    } 

    httpRequest.setParams(httpParams); 

    if (isCancelled()) return null; 

    HttpResponse response = httpClient.execute(httpRequest); 
    httpClient.close(); 

    if (isCancelled()) return null; 

    final int statusCode = response.getStatusLine().getStatusCode(); 

    if (statusCode == 300 || statusCode >= 305) 
    { 
     error = HttpHelper.getStatusCodeMessage(statusCode, getActivity()); 
    } 
    else 
    { 
     if (isCancelled()) return null; 

     HttpEntity entity = response.getEntity(); 

     if (entity != null) 
     { 

      InputStream instream = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8")); 
      StringBuilder sb = new StringBuilder(); 

      String line = null; 
      while ((line = reader.readLine()) != null) 
       sb.append(line); 

      String test = sb.toString(); //that was for debugging the string 
      return test; 

     } 
    } 
} 

이 두 요청은 AsyncTask를 실행하고 있습니다.

종류의 안부 해파리

+0

는 로그'를 통해 로그 아웃 문자열을 쓰고있다. ()'? 로그에는 최대 출력 길이가 있으므로 문자열을 500 자 이하의 문자로 나눠서 모든 것이 인쇄되도록하는 것이 가장 좋습니다. – LeffelMania

+1

[EntityUtils] (http://developer.android.Entity를 문자열로 검색 할 때 유용하여 마지막 10 줄 정도를 필요로하지 않게 해줍니다. –

+0

@LeffelMania : 아니요, 디버깅 옵션으로 중단하고 문자열 길이를 확인했습니다. @ 데이비드 Caunt : 감사합니다! – jellyfish

답변

2

나는 이것이 원인이 확실하지 않다,하지만 의심스러운 -

HttpResponse response = httpClient.execute(httpRequest); 
httpClient.close(); // <-- 

나는 클라이언트를 닫기 전에 HttpEntity 소비 끝날 때까지 기다릴 것입니다.

+0

놀랍게도 내 실수를 너무 빨리 발견했습니다. 너무 감사드립니다! – jellyfish

0

본인은 새로운데, 바보처럼 들리면 용서해주십시오. 는이 기사에서 흥미로운 점을 발견 :.

http://android-developers.blogspot.com/2010/12/new-gingerbread-api-strictmode.html

그것은 당신이 당신의 주 스레드에서 네트워크 요청을하지 않을해야한다 "말했다 사실, 우리는 네트워크 요청을했습니다곧 벌집 릴리스에서 주 스레드에서 치명적인 오류 앱 않는 이 벌집 전에 API 버전을 목표로하고있다 "

은 별도의 ASyncThread에 요청을 실행하고 있습니까? 나는 코드를 보면서 말할 수 없다. 나는이 일을 혼자하는 시간을 갖고있다. 당신이 어떻게했는지보고 싶어서 아무 것도 생각 나지 않으면 알려주세요.

감사

, 앤드류는

+1

해파리, 방금 메모를 보았습니다. 그들은 비동기를 실행하고 있습니다. 사과드립니다. 나는 대답을 스스로 찾으려고 혼란스러워했다. – LunaSkye

관련 문제