android url解析json数据,Android从url解析json并存储它

我支持这个上的什么东西,抓取数据然后序列化到磁盘.

下面的代码显示了第一个阶段,将JSON抓取并解析为JSON对象并保存到磁盘

// 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序列化并保存到磁盘后,您可以随时使用以下命令将其加载回来:

// Load in an object

ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File(new File(getCacheDir(),"")+"cacheFile.srl")));

JSONObject jsonObject = (JSONObject) in.readObject();

in.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值