2011-01-05 71 views
0

在嘗試不同的方法後,我無法從HttpResponse檢索所有數據。無法檢索來自httpResponse的所有數據

這裏是我的代碼:

private static final DefaultHttpClient httpClient = 
    new DefaultHttpClient(); 

public void get() throws Exception { 
    HttpPost httpPost = new HttpPost("https://dt.abc.net/abc.exe"); 

    httpPost.addHeader("Accept", "text/xml"); 
    httpPost.setHeader("Content-Type","application/xml;charset=UTF-8") 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("XML", 
      getRequestTypeStringBuilder())); 

    // In here "XML" is parameter value that is required send to the server 
    // before request data from server. 

    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

    HttpParams params = new BasicHttpParams(); 
    HttpConnectionParams.setStaleCheckingEnabled(params, false); 
    HttpConnectionParams.setConnectionTimeout(params, 5000); 
    HttpConnectionParams.setSoTimeout(params, 5000); 
    httpClient.setParams(params); 
    httpClient.getParams().setParameter(
      ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); 
    HttpResponse response = httpClient.execute(httpPost); 

    InputStream is = response.getEntity().getContent(); 

    ByteArrayOutputStream os = new ByteArrayOutputStream(); 
    byte[] buf; 
    int ByteRead; 
    buf = new byte[1024]; 

    String xmldata = null; 
    while ((ByteRead = is.read(buf, 0, buf.length)) != -1) { 
     os.write(buf, 0, ByteRead); 
     totalSize += ByteRead;      
    } 

    xmldata = os.toString().replaceAll(" ", ""); 
    os.close(); 
    is.close(); 
} 

的一個問題是,如果我設置內容類型=「application/xml進行」,並嘗試檢索從遠程服務器,這是在大量的數據,但它不能檢索所有數據。但是,如果數據相對較小,那麼它就是完美的。

所有返回數據是XML格式

如果我改變內容類型=「應用程序/ x WWW的形式進行了urlencoded」,它將引發和異常的另一個問題:

01-05 18:59:53.165: WARN/System.err(372): javax.net.ssl.SSLProtocolException: Read error: ssl=0x2f9698: Failure in SSL library, usually a protocol error 
01-05 18:59:53.195: WARN/System.err(372):  at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(Native Method) 
01-05 18:59:53.208: WARN/System.err(372):  at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:786) 
01-05 18:59:53.215: WARN/System.err(372):  at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103) 
01-05 18:59:53.236: WARN/System.err(372):  at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134) 
01-05 18:59:53.236: WARN/System.err(372):  at org.apache.http.impl.io.IdentityInputStream.read(IdentityInputStream.java:86) 
01-05 18:59:53.246: WARN/System.err(372):  at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:159) 
01-05 18:59:53.286: WARN/System.err(372):  at com.epsilon.android.dreammailandroid.remoteserver.AsyncDownloadDataInsert.doInBackground(AsyncDownloadDataInsert.java:111) 
01-05 18:59:53.295: WARN/System.err(372):  at com.epsilon.android.dreammailandroid.remoteserver.AsyncDownloadDataInsert.doInBackground(AsyncDownloadDataInsert.java:1) 
01-05 18:59:53.316: WARN/System.err(372):  at android.os.AsyncTask$2.call(AsyncTask.java:185) 
01-05 18:59:53.316: WARN/System.err(372):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 
01-05 18:59:53.335: WARN/System.err(372):  at java.util.concurrent.FutureTask.run(FutureTask.java:138) 
01-05 18:59:53.335: WARN/System.err(372):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 
01-05 18:59:53.346: WARN/System.err(372):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 
01-05 18:59:53.396: WARN/System.err(372):  at java.lang.Thread.run(Thread.java:1019) 
01-05 18:59:55.764: WARN/InputManagerService(61): Window already focused, ignoring focus gain of: [email protected] 

我不能不明白爲什麼我無法獲得所有數據。

你能幫我,找出我的代碼中有什麼問題。

注意:我從android應用程序檢索數據。

謝謝

「願所有的幸福快樂」

問候和慈光, Ichirohang林布

+0

你叫一些UI事件處理這個功能呢? – 2011-01-05 11:28:25

+0

是的,我使用android AsyncTask。它有不同嗎? – user553710 2011-01-05 15:18:52

+0

有沒有人注意到從代碼頂部的第8行有語法錯誤? 'httpPost.setHeader(「Content-Type」,「application/xml; charset = UTF-8」)' – 2011-07-05 05:39:29

回答

0

如果返回的數據爲text/xml,你可以嘗試:

HttpResponse response = httpClient.execute(httpPost); 
String content = EntityUtils.toString(response.getEntity()); 
+0

嗨戴夫,我也嘗試這種方式,但無法工作。我只檢查了大約332千字節的數據。任何建議 – user553710 2011-01-05 11:48:35

+0

您是否嘗試過使用:'HttpPost.setHeader( 「內容類型」, 「應用程序/ x-WWW的形式,進行了urlencoded」);'和'也httpPost.setEntity(新UrlEncodedFormEntity(namevaluepairs中,HTTP.UTF_8)) ;' – 2011-01-05 12:09:01

+0

嗨戴夫,感謝您的快速回復。好吧,我嘗試httpPost.setEntity(new UrlEncodeFormEntity(nameValuePairs,HTTP.UTF_8));但是,在HttpPost.setHeader中設置(「Content-Type」,「application/x-www-form-urlencoded; charset = UTF-8」),它仍然不會返回所有數據。我還在nameValuePairs.add中添加了另一個參數(新的BasicNameValuePair(「LoginId」,「abc」));和nameValuePairs.add(new BasicNameValuePair(「Password」,「123」));它對響應數據有影響嗎?我是否需要在標題中設置內容長度?任何建議 – user553710 2011-01-05 15:14:34

0

該錯誤指示SSL級別或網絡問題。這些問題都發生在Http協議錯誤下面的一層,我懷疑上面的連接類型建議可能沒有密切關係。

javax.net.ssl.SSLProtocolException:讀取錯誤:ssl = 0x2f9698:SSL庫失敗,通常是協議錯誤 01-05 18:59:53.195:WARN/System.err(372):at org。 apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(本機方法) 01-05 18:59:53.208:WARN/System.err的(372):在org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl $ SSLInputStream.read(OpenSSLSocketImpl.java:786)

0

試圖檢查,如果它不超時