2012-09-12 74 views
0

我有我需要接收一個常量分塊JSON響應的情況,每當我收到響應時,我需要解析JSON以更新UI的當前操作狀態。Android接收分塊的JSON響應

我找不到一個單獨接收每個響應的直接方法,並將信息發回到用戶界面,直到響應完成接收。

如果任何人都可以對此有所瞭解,我將不勝感激!

我有101種不同的方式進行http調用。我只需要正確的方法來讀取響應。

- EDIT - 只需添加到此。我有不讀書JSON數據的問題,它是單獨獲取每個數據塊並回發到各塊之間的界面 - NathofGod 27秒前編輯

 DefaultHttpClient http_client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     HttpResponse response = http_client.execute(httpGet); 

     StatusLine statusLine = response.getStatusLine(); 
     int statusCode = statusLine.getStatusCode(); 
     HttpEntity entity = response.getEntity(); 

     InputStream in = entity.getContent(); 
     StringBuffer out = new StringBuffer(); 
     byte[] b = new byte[4096]; 
     int n = in.read(b); 
     out.append(new String(b, 0, n));   
     String resultdata = out.toString(); 

一些示例JSON

23a 
{"header":{"status":{"code":0,"desc":"Ok"},"sessionId":"3olpgjktvj0a52dbim6lk8hvet"},"summary":{"status":{"code":0,"desc":"Ok"},"inProgess":18,"errored":0},"storeItems":[{"storeItemId":"4ff41f0bdc6d34b3daaf4d8a","name":"Mcvities Digestive 250G","quantity":1,"itemPrice":0.89,"totalPrice":0.89,"recipeIngredients":[{"name":"250 g/8¾oz digestive biscuits, crushed","amount":1.0,"recipe":{"url":"http://www.bbc.co.uk/food/recipes/baileysandchocolatec_72293","title":"Baileys and chocolate cheesecake"}}],"status":{"code":3101,"desc":"Store item send to supermarket Ok"}}]} 
3c1 
{"header":{"status":{"code":0,"desc":"Ok"},"sessionId":"3olpgjktvj0a52dbim6lk8hvet"},"summary":{"status":{"code":0,"desc":"Ok"},"inProgess":17,"errored":0},"storeItems":[{"storeItemId":"4ff41f0bdc6d34b3daaf4d8a","name":"Mcvities Digestive 250G","quantity":1,"itemPrice":0.89,"totalPrice":0.89,"recipeIngredients":[{"name":"250 g/8¾oz digestive biscuits, crushed","amount":1.0,"recipe":{"url":"http://www.bbc.co.uk/food/recipes/baileysandchocolatec_72293","title":"Baileys and chocolate cheesecake"}}],"status":{"code":3101,"desc":"Store item send to supermarket Ok"}},{"storeItemId":"4ff41a73dc6d34b3daaf46da","name":"Rachels Organic Unsalted Butter 250G","quantity":1,"itemPrice":1.65,"totalPrice":1.65,"recipeIngredients":[{"name":"100 g/3½oz butter","amount":0.4,"recipe":{"url":"http://www.bbc.co.uk/food/recipes/baileysandchocolatec_72293","title":"Baileys and chocolate cheesecake"}}],"status":{"code":3101,"desc":"Store item send to supermarket Ok"}}]} 
550 
{"header":{"status":{"code":0,"desc":"Ok"},"sessionId":"3olpgjktvj0a52dbim6lk8hvet"},"summary":{"status":{"code":0,"desc":"Ok"},"inProgess":16,"errored":0},"storeItems":[{"storeItemId":"4ff41f0bdc6d34b3daaf4d8a","name":"Mcvities Digestive 250G","quantity":1,"itemPrice":0.89,"totalPrice":0.89,"recipeIngredients":[{"name":"250 g/8¾oz digestive biscuits, crushed","amount":1.0,"recipe":{"url":"http://www.bbc.co.uk/food/recipes/baileysandchocolatec_72293","title":"Baileys and chocolate cheesecake"}}],"status":{"code":3101,"desc":"Store item send to supermarket Ok"}},{"storeItemId":"4ff41a73dc6d34b3daaf46da","name":"Rachels Organic Unsalted Butter 250G","quantity":1,"itemPrice":1.65,"totalPrice":1.65,"recipeIngredients":[{"name":"100 g/3½oz butter","amount":0.4,"recipe":{"url":"http://www.bbc.co.uk/food/recipes/baileysandchocolatec_72293","title":"Baileys and chocolate cheesecake"}}],"status":{"code":3101,"desc":"Store item send to supermarket Ok"}},{"storeItemId":"4ff417c0dc6d34b3daaf28d3","name":"Menier Milk Chocolate Patissier 100G","quantity":1,"itemPrice":1.0,"totalPrice":1.0,"recipeIngredients":[{"name":"100 g/3½oz grated chocolate","amount":1.0,"recipe":{"url":"http://www.bbc.co.uk/food/recipes/baileysandchocolatec_72293","title":"Baileys and chocolate cheesecake"}}],"status":{"code":3101,"desc":"Store item send to supermarket Ok"}}]} 
+0

從API 11開始,有一個JSONReader類爲此提供了一些有用的方法,但是它太高了? – harism

+0

我很好,閱讀JSON數據,它只是每次接收一個數據塊併發布到中間的用戶界面 – NathofGod

+0

是的,我瞭解到,JSONReader似乎是一種可用於此目的的解析器。 – harism

回答

0
int n = in.read(b); 
while(n>0){ 
    out.append(new String(b, 0, n); 
    n = in.read(b); 
} 

String resultData = out.toString();