【发布时间】:2014-04-02 06:51:03
【问题描述】:
我有一个很大的 JSON 输入 (download the file) API,但我不知道如何解析这些数据。我需要:
- 将此数据(整个 JSON 输入)保存到文本文件或数据库。最好的方法是什么?
- 从文本文件或数据库中加载此数据,并从 JSON 标签“list”(第一个标签)创建 JSONArray
该解决方案应该很快并且支持 Android 2.3。你对此有什么建议?有什么想法吗?
我的代码:
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
HttpResponse httpResponse;
httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
... and what next ?...
仅供参考:
EntityUtils 抛出 OutOfMemoryException
编辑:
我尝试像这样将数据保存到文件中:
InputStream inputStream = httpEntity.getContent();
FileOutputStream output = new FileOutputStream(Globals.fileNews);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
没关系。我加载数据:
FileInputStream fis = null;
StringBuffer fileContent = new StringBuffer("");
fis = new FileInputStream(Globals.fileNews);
byte[] buffer = new byte[1024];
while (fis.read(buffer) != -1) {
fileContent.append(new String(buffer));
}
但是如何将StringBuffer 转换为JSONObject? fileContent.ToString() 不理想,有时我得到OutOfMemoryException。
【问题讨论】:
-
JSON 被用于动态处理。试试这个答案:link
标签: android json performance http get