2011-07-05 6 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。)があります。エンティティを文字列として取得するのに便利なので、最後の10行程度を必要としないように保存しておくと便利です(com/reference/org/apache/http/util/EntityUtils.html)。 –

+0

@LeffelMania:いいえ、私はデバッグオプションで停止し、そこで文字列長をチェックしました。 @デイヴィッドカント:ありがとう! – 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

:。 は、私がこの記事で興味深いポイントを見つけましたあなたのアプリが

あなたが別のASyncThreadでリクエストを実行しています」ハニカム前に、APIのバージョンをターゲットにされていない限り、メインスレッドの致命的なエラーの?私はそのコードを見て話すことができません。私はこれを自分自身でやっている時間のdoozieを持っています。あなたが何をしたかを私が大好きに思うように、あなたが何かを思い付いたら教えてください。

敬具、 アンドリュー

+1

クラゲ、私はちょうどあなたのメモを見ました。彼らは非同期を実行しています。謝罪します。私はちょうど答えを探して自分自身を困惑しています。 – LunaSkye

関連する問題