从Web服务中检索JSON并解析它的最简单方法是什么?没有使用任何额外的插件?检索和解析JSON
0
A
回答
0
0
你应该看看这里:http://code.google.com/p/android-query/wiki/AsyncAPI
一切都解释的,你需要的代码示例。
0
下面的代码应该给你一个出发点,只是在活动/服务类中使用它。它从一个URL(web服务)获取数据,并将其转换为可以使用的JSON对象。
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(***Put your web service URL here ***)
try
{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200)
{
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null)
{
stringBuilder.append(line);
}
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
//Turn string JSON into object you can process
JSONArray jsonArray = new JSONArray(stringBuilder.toString());
for (int i = 0; i < jsonArray.length(); i++)
{
//Get Each element
JSONObject jsonObject = jsonArray.getJSONObject(i);
//Do stuff
}
2
相关问题
- 1. 文本解析和检索
- 2. 如何解析从URL检索的JSON?
- 3. JSON解析[和]
- 4. JSON解析PHP搜索API
- 5. GSON解析索引的JSON
- 6. 解析JSON Twitter搜索
- 7. 从ViewState解析和检索属性?
- 8. 数据属性检索和解析javascript
- 9. JSON API响应检索值和解码
- 10. jQuery和JSON解析
- 11. Android和Json解析
- 12. G2和json解析
- 13. UWP和JSON解析
- 14. Android和json解析
- 15. 解析XML时检索XML
- 16. 解析检索错误PFFile
- 17. 解析HTML检索标签
- 18. ASIHTTPRequest和ASINetworkQueue和JSON解析
- 19. 使用AJAX检索的Json代码不可解析
- 20. 限制解析JSON细节时检索到的对象数
- 21. 解析JSON消息以检索iOS上的Twitter数据5
- 22. 从json中的URL中检索到的解析响应
- 23. 检索以.json从资产文件,并解析
- 24. 解析嵌套的JSON来检索嵌套的数组值
- 25. 增强属性树不能检索解析json后的记录
- 26. 用jQuery解析JSON,检索两个变量。
- 27. 解析并从JSON中检索信息 - JSONObject与JSONArray?
- 28. HTTP POST和Scrapy解析JSON
- 29. jquery,Ajax和JSON解析undefined
- 30. 用GWT和GAE解析JSON?