0

I have situation where I need to receive a constant chunked JSON response, each time I receive a response I need to parse the JSON to update the UI on the current status of the operation.

I can't find a straight forward method of received each response individually and post the information back to the UI until there response has finished receiving.

If anyone could shed some light on this I'd be grateful !!

I have 101 different ways of doing the http call.. I just need the correct one with the correct way to read the response.

--EDIT -- Just to add to this. The problem I am having is not reading JSON data, it is getting each data chunk separately and posting back to the UI between each chunk – NathofGod 27 secs ago edit

        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();

Some Example 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"}}]}
NathofGod
  • 479
  • 5
  • 12
  • Since API 11 there's been a class JSONReader which provides some useful methods for this purpose but is it too high? – harism Sep 13 '12 at 00:40
  • I'm fine reading the JSON data, it's just receiving each chunk of data one at a time and posting to the UI inbetween – NathofGod Sep 13 '12 at 20:56
  • Yup, I understood that and JSONReader seems to be sort of a pull parser you could use for that purpose. – harism Sep 13 '12 at 21:00

1 Answers1

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

String resultData = out.toString();
xandy
  • 27,357
  • 8
  • 59
  • 64