2013-06-03 18 views
4

我有一些代碼可以下載已付費應用幾年的XML文件 - 從來沒有任何麻煩,直到我最近發現這種情況發生在HTC One中。HTC One bug?出現在URLConnection InputStream中的HTTP頭部分

想知道是否有我丟失的東西,或者如果這應該報告某處。

下面是示例代碼,迫使問題:

package com.lutron.davetest; 

import java.io.BufferedInputStream; 
import java.io.InputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.view.Menu; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    private TextView mainCenterText; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mainCenterText = (TextView) findViewById(R.id.mainCenterText); 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Enter the IP address"); 
     final EditText edit = new EditText(this); 
     builder.setView(edit); 
     builder.setPositiveButton("OK", new OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       mainCenterText.setText(""); 
       String ip = edit.getText().toString(); 
       new DownloadTask().execute(ip); 
      } 
     }); 
     builder.create().show(); 
    } 

    private class DownloadTask extends AsyncTask<String, Void, Void>{ 

     @Override 
     protected Void doInBackground(String... params) { 
      try{ 
       URL url = new URL("http", params[0], 80, "/file.xml"); 
       URLConnection connection = url.openConnection(); 
       InputStream in = new BufferedInputStream(connection.getInputStream()); 
       byte buf[] = new byte[4096]; 
       int len; 
       while((len = in.read(buf)) != -1) { 
        final String data = new String(buf, 0, len); 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          mainCenterText.append(data); 
         } 
        }); 
       } 

      }catch(Exception e){ 
       e.printStackTrace(); 
      } 
      return null; 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

什麼情況是,從流您只能獲得該文件的內容讀取時所有其他設備上。

在HTC One之後得到這個,然後將文件內容:

onnection: close 

Last-Modified: ??? 

Content-Type: text/html 

這與Android 4.1.2。

有沒有辦法配置URLConnection來避免這種情況?

回答

1

哇。

深入研究這個問題,我們意識到這隻發生在特定的頁面上,並且驗證了當這款手機在瀏覽器中瀏覽它們所服務的任何頁面時,這些HTTP標頭仍然出現在主體中,導致錯誤。

今天我發了一個小HTTP會議,發現這些頭文件正在被認爲是身體的一部分!

HTTP層中的所有換行符都是\ n \ r或LF CR。

從HTTP 1.1 RFC 2616:http://www.ietf.org/rfc/rfc2616.txt

HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all 
protocol elements except the entity-body (see appendix 19.3 for 
tolerant applications). The end-of-line marker within an entity-body 
is defined by its associated media type, as described in section 3.7. 

    CRLF   = CR LF 

... 

    […] a bare CR 
or LF MUST NOT be substituted for CRLF within any of the HTTP control 
structures (such as header fields and multipart boundaries). 

... 

6 Response 

After receiving and interpreting a request message, a server responds 
with an HTTP response message. 

    Response  = Status-Line    ; Section 6.1 
        *((general-header  ; Section 4.5 
        | response-header  ; Section 6.2 
        | entity-header) CRLF) ; Section 7.1 
        CRLF 
        [ message-body ]   ; Section 7.2 

這似乎是一個很大的問題 - 但是,應用程序可能持續到現在的唯一方法似乎是應用要容忍一個LF的建議(19.3) 。

所以我想HTC忽略了HTC One上的這個建議! :-P

爲了解決這個問題,我嘗試使用HttpClient(Android SDK包含的版本),但也有同樣的問題。我想嘗試使用來自Apache HTTP組件的jar文件,但是當然那些包名稱與Android框架中的版本衝突,所以我使用Jar Jar Links對它們進行了重新打包。一旦進口替換它的工作!