我遇到需要接收恒定分块 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"}}]}