2011-12-30 135 views
-1

**上請求服務器準備和響應發送整個數據(JSON)到Android客戶端**

{"property":[{"prop_Age":0,"prop_Area":300,"prop_Area_SqFeet":2700,"prop_Area_Unit":" Yards","prop_AvailableFor":"Sale","prop_Desc":"Ashok Vihar Phase-1, Individual House for Sale, Corner Freehold 300g Plot in (i) Block Ashok Vihar Phase I @rs 15 Cr","prop_ID":341,"prop_LastUpdatedDate":"","prop_Lease_ID":0,"prop_NumOfBaths":3,"prop_NumOfBeds":3,"prop_OnFloor":0,"prop_PostedDate":"2011-08-01","prop_Price":1.5E8,"prop_Title":"Ashok Vihar Phase-1, Individual House fo","propertyImages":[{"imageID":1042,"imageUrl":"http://localhost/barun/images/thumb.jpg","propertyID":341,"type":"thumb"},{"imageID":1043,"imageUrl":"http:////localhost/barun/images/gallery.jpg","propertyID":341,"type":"gallery"},{"imageID":1044,"imageUrl":"http:////localhost/barun/images/full.jpg","propertyID":341,"type":"full"}],"propertyLocations":{"proploc_AddressLine1":"ashok vihar phase .............................爲什麼Android會收到損壞的數據?


* ANDROID接收數據,並從響應中讀取數據,但不能收到原始內容
一些數據截斷從原始響應 *

12-30 17:07:44.599: I/System.out(4348): Buffer data 
is{"property":[{"prop_Age":0,"prop_Area":300,"prop_Area_SqFeet":2700,"prop_Area_Unit":" 
Yards","prop_AvailableFor":"Sale","prop_Desc":"Ashok Vihar Phase-1, Individual House for Sale, Corner Freehold 300g Plot in 
(i) Block Ashok Vihar Phase I @rs 15 Cr","prop_ID":341,"prop_LastUpdatedDate":"","prop_Lease_ID":0,"prop_NumOfBaths":3,"prop_NumOfBeds":3,"prop_OnFloor":0,"prop_P 
ostedDate":"2011-08-01","prop_Price":1.5E8,"prop_Title":"Ashok Vihar Phase-1, Individual House 
fo","propertyImages":{"phoneNumber1":0,"phoneNumber2":0,"usr_Email":"","usr_ID":341,"usr_����������������������������������������� 
����������������������������������������������������������������������������������������������������������������������������� 
����������������������������������������������������������������������������������������������������������������������������� 
����������������������������������������������������������������������������������������������������������������������������� 
����������������������������������������������������������������������������������������������������������������������������� 
�����������������������������������������������������������������������������������������������������������������������������` 

ANDROID代碼來接收數據:

int bytesRead = -1; 
StringBuffer str = new StringBuffer(); 
byte[] bufferTest = new byte[1*1024]; 
try { 
    while ((bytesRead = instream.read(bufferTest)) >= 0) { 
     String line = new String(bufferTest); 
     str.append(line.toString()); 
    } 
} catch (IOException e1) { 
    // TODO Auto-generated catch block 
    e1.printStackTrace(); 
} 
System.out.println("Buffer data is" + str); 

爲什麼Android客戶端收到損壞的格式的數據?

+0

您應該進一步描述錯誤。也許刪除所有的二進制(?)內容...... – home 2011-12-30 12:53:37

+0

所以..你正在從字節流中讀取(使用上帝知道什麼字符集)並將其轉儲到字符串中,而不指定字節是什麼,不關心讀取的字節數?你期望會發生什麼?使用'InputStreamReader',指定字符集並使用它來代替... – Jens 2011-12-30 13:00:12

+1

請不要關閉它。用戶似乎是新的,並不知道要發佈什麼。他可能會編輯該問題。我遇到過類似的問題我有一個解決方案 – 2011-12-30 13:19:56

回答

2

我有一個類似issue.The原因,你看到垃圾數據是已分配的1024個字節,但lesser.So字節的其餘部分都堆滿了垃圾

您可以通過一定解決它實際的數據是多少執行以下操作

int actual = 0; 
    int length = 1024; 
    responseBody = new byte[length]; 
    while ((bytesread < length) && (actual != -1)) { 
     actual = instream.read(responseBody, bytesread, length - bytesread); // 
     // Logger.log("After is.read(), actual is " + actual); 
     if (actual != -1) { 
      bytesread += actual; 
     } 
    } 
    String response = new String(responseBody, "UTF-8"); 

(或者,對於作品很多更好的解決方案)

// Assuming your character set is UTF-8 
InputStreamReader reader = new InputStreamReader(instream, "UTF-8"); 
StringBuilder b = new StringBuilder(); 
try { 
    char[] buffer = new char[1024]; 
    int count; 
    while ((count = reader.read(buffer)) != -1) { 
     b.append(buffer, 0, count); 
    } 
} finally { 
    reader.close(); 
} 
String response = b.toString(); 

讓我知道發生了什麼。

+0

我已經嘗試了兩個代碼,但有時數據進來:原創內容+損壞的內容+原創內容,所以我無法接收所有原始內容 – 2011-12-31 05:21:47