从URL,并将其存储的Andr​​oid解析JSON [英] Android parse json from url and store it

查看:132
本文介绍了从URL,并将其存储的Andr​​oid解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我创建我的第一个Android应用程序,我想知道什么是理想我想储存的地方,所以我可以继续下去的分析从一个URL.Also的JSON饲料的最佳和最有效的方法回到它在应用程序的不同部分。我已经到处找,发现大量的做这件事的不同方式,我不知道哪去了。在你看来什么高效,便捷地解析JSON的最好方法?

Hi there i'm creating my first android app and i'm wanting to know what is the best and most efficient way of parsing a JSON Feed from a URL.Also Ideally i want to store it somewhere so i can keep going back to it in different parts of the app. I have looked everywhere and found lots of different ways of doing it and i'm not sure which to go for. In your opinion whats the best way of parsing json efficiently and easily?

推荐答案

我会与whatsthebeef一边就这一个,获取数据,然后序列化到磁盘。

I'd side with whatsthebeef on this one, grab the data and then serialize to disk.

在code以下显示了第一个阶段,抓住和解析您的JSON成JSON对象,并保存到磁盘

The code below shows the first stage, grabbing and parsing your JSON into a JSON Object and saving to disk

// Create a new HTTP Client
DefaultHttpClient defaultClient = new DefaultHttpClient();
// Setup the get request
HttpGet httpGetRequest = new HttpGet("http://example.json");

// Execute the request in the client
HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
// Grab the response
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();

// Instantiate a JSON object from the request response
JSONObject jsonObject = new JSONObject(json);

// Save the JSONOvject
ObjectOutput out = new ObjectOutputStream(new FileOutputStream(new File(getCacheDir(),"")+"cacheFile.srl"));
out.writeObject( jsonObject );
out.close();

一旦你的JSONObject的序列化并保存到磁盘,您可以使用加载它早在任何时间:

Once you have the JSONObject serialized and save to disk, you can load it back in any time using:

// Load in an object
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));
JSONObject jsonObject = (JSONObject) in.readObject();
in.close();

这篇关于从URL,并将其存储的Andr​​oid解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆