Android接收分块JSON响应

时间:2012-09-12 23:47:52

标签: android json http progress chunked

我遇到需要接收常量分块JSON响应的情况,每次收到响应时我需要解析JSON以更新操作当前状态的UI。

我找不到单独收到每个回复的直接方法,并将信息发回UI,直到响应完成接收为止。

如果有人能对此有所了解,我将不胜感激!!

我有101种不同的方式来进行http调用..我只需要正确的方法来读取响应。

- 编辑 - 只是添加到此。我遇到的问题不是读取JSON数据,而是分别获取每个数据块并在每个块之间回发到UI - 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"}}]}

1 个答案:

答案 0 :(得分:0)

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

String resultData = out.toString();