2011-07-05 23 views
2

我遇到了一個非常奇怪的問題,我沒有絲毫的想法從哪裏開始。作爲http響應中斷的字符串? - Java,Platform Android 3.0

我發送一個http請求到服務器並得到一個簡單的字符串作爲響應。這在我的智能手機應用中運行良好;它甚至能在我的瀏覽器中正常工作。但是,雖然我認爲我只是複製並粘貼了智能手機代碼,但它不適用於我的平板電腦(Android 3.0.1)版本的應用程序。

我檢查了調試器,舊版本獲取長度爲2958個字符的字符串。不過,新版本只能獲得長度爲1334的字符串。我記錄了新版本的URL,將其放入我的瀏覽器並再次獲得了2985個字符的字符串。

我真的找不到我的代碼有任何重大區別(請參閱下文)。另外,我不相信Android會有一些改變來限制字符串的長度?!

那麼有沒有人有想法?


原廠智能手機代碼:

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。com/reference/org/apache/http/util/EntityUtils.html)用於以字符串形式檢索實體,從而不需要最後10行。 –

+0

@LeffelMania:不,我停止了調試選項,並在那裏檢查了字符串長度。 @David 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?我無法通過查看代碼來判斷。我有一個時間做自己的doozie。請讓我知道,如果你想出任何東西,我會很樂意看到你是如何做到的。

真誠, 安德魯

+1

水母,我剛看到你的筆記。他們正在運行異步。我道歉。我只是心煩意亂尋找答案。 – LunaSkye