数据格式为

  1. {"sid":"737",
    "tts":"http:\/\/news.iciba.com\/admin\/tts\/2013-12-11.mp3",
    "content":"I don't want us to be together because we have to,I want us to be together because we want to.",
    "note":"\u6211\u4e0d\u5e0c\u671b\u6211\u4eec\u56e0\u4e3a\u201c\u4e0d\u5f97\u4e0d\u201d\u800c\u5728\u4e00\u8d77\uff0c\u6211\u5e0c\u671b\u6211\u4eec\u662f\u56e0\u4e3a\u60f3\u5728\u4e00\u8d77\u800c\u5728\u4e00\u8d77\u3002",
    "translation":"\u611f\u8c22@\u7a0b\u5f88\u591a\u8981\u79d2\u8650\u6570\u5b66 \u6295\u7a3f\u3002\u8bcd\u9738\u5c0f\u7f16\uff0c\u8fd9\u53e5\u8bdd\u6765\u81ea\u300a\u51b0\u6cb3\u4e16\u7eaa2\u300b\uff0c\u662f\u4e00\u4e2a\u7cfb\u5217\u7684\u52a8\u753b\u7535\u5f71\uff0c\u975e\u5e38\u641e\u7b11\uff0c\u4f60\u770b\u8fc7\u5417\uff1f",
    "picture":"http:\/\/cdn.iciba.com\/news\/word\/2013-12-11.jpg","picture2":"http:\/\/cdn.iciba.com\/news\/word\/big_2013-12-11b.jpg","caption":"\u8bcd\u9738\u6bcf\u65e5\u4e00\u53e5",
    "dateline":"2013-12-11",
    "s_pv":"8693",
    "sp_pv":"2090",
    "tags":[{"id":"9","name":"\u7231\u60c5"},{"id":"14","name":"\u7535\u5f71\u7ecf\u5178"}],
    "fenxiang_img":"http:\/\/cdn.iciba.com\/web\/news\/longweibo\/imag\/2013-12-11.jpg"}

JSON字段解释

  1. JSON 字段解释
  2. {
  3. 'sid':'' #每日一句ID
  4. 'tts': '' #音频地址
  5. 'content':'' #英文内容
  6. 'note': '' #中文内容
  7. 'translation':'' #词霸小编
  8. 'picture': '' #图片地址
  9. 'picture2': '' #大图片地址
  10. 'caption':'' #标题
  11. 'dateline':'' #时间
  12. 's_pv':'' #浏览数
  13. 'sp_pv':'' #语音评测浏览数
  14. 'tags':'' #相关标签
  15. 'fenxiang_img':'' #合成图片,建议分享微博用的
  16. }

最终实现的效果

具体实现,使用AsynTask异步访问网络:

  1. class Load extends AsyncTask<String, String, String>
  2. {
  3. public String url = "http://open.iciba.com/dsapi/";
  4. ProgressDialog pdlg;
  5. String jsonstr = "";
  6. JSONObject json = null;
  7. @Override
  8. protected String doInBackground(String... params) {
  9. // TODO Auto-generated method stub
  10. try{
  11. DefaultHttpClient httpClient = new DefaultHttpClient();
  12. HttpPost httppost = new HttpPost(url);
  13. HttpResponse httpResponse = httpClient.execute(httppost);
  14. HttpEntity httpEntity = httpResponse.getEntity();
  15. InputStream is = httpEntity.getContent();
  16. BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
  17. StringBuilder sb = new StringBuilder();
  18. String line = null;
  19. while ((line = reader.readLine()) != null)
  20. {
  21. sb.append(line + "\n");
  22. }
  23. is.close();
  24. jsonstr = sb.toString();
  25. json = new JSONObject(jsonstr.toString());
  26. engstr = json.getString("content");
  27. chistr = json.getString("note");
  28. imagurl = json.getString("picture");
  29. timestr = json.getString("dateline");
  30. fromstr = json.getString("translation");
  31. JSONArray array = json.getJSONArray("tags");
  32. for(int i=0;i<array.length();i++)
  33. {
  34. JSONObject tag = (JSONObject)array.get(i);
  35. tagstr += tag.getString("name")+"," ;
  36. }
  37. }catch(Exception e)
  38. {
  39. e.printStackTrace();
  40. }
  41.  
  42. return null;
  43. }
  44.  
  45. @Override
  46. protected void onPostExecute(String result) {
  47. // TODO Auto-generated method stub
  48. super.onPostExecute(result);
  49. pdlg.dismiss();
  50. imageLoader.DisplayImage(imagurl,imageview);
  51. eng.setText(" "+engstr);
  52. chi.setText(" "+chistr);
  53. tag.setText(tagstr);
  54. time.setText(timestr);
  55. from.setText(" "+fromstr);
  56. }
  57.  
  58. @Override
  59. protected void onPreExecute() {
  60. // TODO Auto-generated method stub
  61. super.onPreExecute();
  62. pdlg = new ProgressDialog(context);
  63. pdlg.setCancelable(false);
  64. pdlg.setMessage("正在加载");
  65. pdlg.show();
  66. }
  67. }

使用了一个图片处理的工具类,ImageLoader,主要用来通过url解析图片,处理图片的大小,以文件的形式缓存图片。

  1. public class ImageLoader {
  2.  
  3. MemoryCache memoryCache=new MemoryCache();
  4. FileCache fileCache;
  5. private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
  6. ExecutorService executorService;
  7.  
  8. public ImageLoader(Context context){
  9. fileCache=new FileCache(context);
  10. executorService=Executors.newFixedThreadPool(5);
  11. }
  12.  
  13. final int stub_id = R.drawable.drug_trans;
  14. public void DisplayImage(String url, ImageView imageView)
  15. {
  16. imageViews.put(imageView, url);
  17. Bitmap bitmap=memoryCache.get(url);
  18. if(bitmap!=null)
  19. imageView.setImageBitmap(bitmap);
  20. else
  21. {
  22. queuePhoto(url, imageView);
  23. imageView.setImageResource(stub_id);
  24. }
  25. }
  26.  
  27. private void queuePhoto(String url, ImageView imageView)
  28. {
  29. PhotoToLoad p=new PhotoToLoad(url, imageView);
  30. executorService.submit(new PhotosLoader(p));
  31. }
  32.  
  33. private Bitmap getBitmap(String url)
  34. {
  35. File f=fileCache.getFile(url);
  36.  
  37. //from SD cache
  38. Bitmap b = decodeFile(f);
  39. if(b!=null)
  40. return b;
  41.  
  42. //from web
  43. try {
  44. Bitmap bitmap=null;
  45. URL imageUrl = new URL(url);
  46. HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
  47. conn.setConnectTimeout(30000);
  48. conn.setReadTimeout(30000);
  49. conn.setInstanceFollowRedirects(true);
  50. InputStream is=conn.getInputStream();
  51. OutputStream os = new FileOutputStream(f);
  52. Utils.CopyStream(is, os);
  53. os.close();
  54. bitmap = decodeFile(f);
  55. return bitmap;
  56. } catch (Exception ex){
  57. ex.printStackTrace();
  58. return null;
  59. }
  60. }
  61.  
  62. //decodes image and scales it to reduce memory consumption
  63. private Bitmap decodeFile(File f){
  64. try {
  65. //decode image size
  66. BitmapFactory.Options o = new BitmapFactory.Options();
  67. o.inJustDecodeBounds = true;
  68. BitmapFactory.decodeStream(new FileInputStream(f),null,o);
  69.  
  70. //Find the correct scale value. It should be the power of 2.
  71. final int REQUIRED_SIZE=70;
  72. int width_tmp=o.outWidth, height_tmp=o.outHeight;
  73. int scale=1;
  74. while(true){
  75. if(width_tmp/1.5<REQUIRED_SIZE || height_tmp/1.5<REQUIRED_SIZE)
  76. break;
  77. width_tmp/=1.5;
  78. height_tmp/=1.5;
  79. scale*=1.5;
  80. }
  81.  
  82. //decode with inSampleSize
  83. BitmapFactory.Options o2 = new BitmapFactory.Options();
  84. o2.inSampleSize=scale;
  85. return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
  86. } catch (FileNotFoundException e) {}
  87. return null;
  88. }
  89.  
  90. //Task for the queue
  91. private class PhotoToLoad
  92. {
  93. public String url;
  94. public ImageView imageView;
  95. public PhotoToLoad(String u, ImageView i){
  96. url=u;
  97. imageView=i;
  98. }
  99. }
  100.  
  101. class PhotosLoader implements Runnable {
  102. PhotoToLoad photoToLoad;
  103. PhotosLoader(PhotoToLoad photoToLoad){
  104. this.photoToLoad=photoToLoad;
  105. }
  106.  
  107. @Override
  108. public void run() {
  109. if(imageViewReused(photoToLoad))
  110. return;
  111. Bitmap bmp=getBitmap(photoToLoad.url);
  112. memoryCache.put(photoToLoad.url, bmp);
  113. if(imageViewReused(photoToLoad))
  114. return;
  115. BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
  116. Activity a=(Activity)photoToLoad.imageView.getContext();
  117. a.runOnUiThread(bd);
  118. }
  119. }
  120.  
  121. boolean imageViewReused(PhotoToLoad photoToLoad){
  122. String tag=imageViews.get(photoToLoad.imageView);
  123. if(tag==null || !tag.equals(photoToLoad.url))
  124. return true;
  125. return false;
  126. }
  127.  
  128. //Used to display bitmap in the UI thread
  129. class BitmapDisplayer implements Runnable
  130. {
  131. Bitmap bitmap;
  132. PhotoToLoad photoToLoad;
  133. public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
  134. public void run()
  135. {
  136. if(imageViewReused(photoToLoad))
  137. return;
  138. if(bitmap!=null)
  139. photoToLoad.imageView.setImageBitmap(bitmap);
  140. else
  141. photoToLoad.imageView.setImageResource(stub_id);
  142. }
  143. }
  144.  
  145. public void clearCache() {
  146. memoryCache.clear();
  147. fileCache.clear();
  148. }
  149.  
  150. }

Android 图文数据JSON解析的更多相关文章

  1. Android 图文数据JSON解析,金山词霸每日一句API的调用

    金山词霸开发的免费API http://open.iciba.com/dsapi/ 数据格式为 {","name":"\u7535\u5f71\u7ecf\u5 ...

  2. Android总结之json解析(FastJson Gson 对比)[申明:来源于网络]

    Android总结之json解析(FastJson Gson 对比)[申明:来源于网络] 地址:http://blog.csdn.net/u014031072/article/details/5392 ...

  3. Android开发之json解析

    目前正在尝试着写app,发现看懂代码和能写出来差距很大,最关键的是java基础比较的差,因为只会python,java基础只学习了一个礼拜就过了.感觉java写出来的代码不如python简单明了. 上 ...

  4. Android总结之json解析(FastJson Gson 对比)

    前言: 最近为了统一项目中使用的框架,发现项目中用到了两种json解析框架,他们就是当今非常主流的json解析框架:google的Gson 和阿里巴巴的FastJson,为了废除其中一个所以来个性能和 ...

  5. Android 中的Json解析工具fastjson 、序列化、反序列化

    Android中通常需要访问服务器,然而服务器返回的数据很多时候都是Json格式 1.fastjson简介 阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备 ...

  6. [转载]Android版本更新与JSON解析

    /*  *注意,这篇文章转载自:  *http://blog.csdn.net/xjanker2/article/details/6303937  *一切权利归作者所有,这里只是转载,曾经用到过这篇文 ...

  7. 常用json解析库比较及选择 fastjson & gson

    一.常用json解析库比较及选择 1.简介 fastjson和gson是目前比较常用的json解析库,并且现在我们项目代码中,也在使用这两个解析库. fastjson 是由阿里开发的,号称是处理jso ...

  8. Android中使用Gson解析JSON数据的两种方法

    Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下   Json是一种类似于XML的通用数据交换格式,具有比XML更高的 ...

  9. android基础---->JSON数据的解析

    上篇博客,我们谈到了XML两种常用的解析技术,详细可以参见我的博客(android基础---->XMl数据的解析).网络传输另外一种数据格式JSON就是我们今天要讲的,它是比XML体积更小的数据 ...

随机推荐

  1. iOS 常用控件 参数

    1.StatusBar 20px 2.TableViewCell 44px 3.TabBar 49px 4.NavigationBar 44px 5.NaviBarIcon 20*20px 6.Tab ...

  2. Shell 编程基础之 Case 练习

    一.语法 case $变量 in "第一个变量内容") # 每个变量内容建议用双引号括起来,关键字则为小括号 ) # 执行内容 ;; # 每个类别结尾使用两个连续的分号来处理! & ...

  3. WPF:依赖属性的数据绑定

    One of the strengths of WPF is its data binding capabilities. Although data binding is not new (in f ...

  4. jq对象转为dom对象:$(".div1")[0] dom对象转为jq对象:$(dom对象)

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  5. topcoder SRM 617 DIV2 SlimeXSlimonadeTycoon

    此题需要注意的两个地方是 (1)在某天生产出来的Slimonades,必须在stale_limit天内必须卖完,否则超过stale_limit内抛弃(东西都有保质期) (2)每天生产出来的Slimon ...

  6. [Cocos2d-x For WP8]Layer 层

        层(CCLayer) 从概念上说,层就是场景里的背景. CCLayer同样是CCNode的子类,通常用addChild方法添加子节点.CCLayer对象定义了可描绘的区域,定义了描绘的规则.C ...

  7. ihhh题解

    10分做法: 由于空间卡得紧,所以给了10分暴力分0.0所以大家很容易就知道暴力就是线段树套ac自动机辣时间:$O((\sum |qSi| + \sum |nSi|)*log Q)$空间:$O((\s ...

  8. 在Eclipse中使用JSHint检查JavaScript

    之前使用 JSlint 来校验 JavaScript 代码,发现灵活性不够,因此改用 JSHint.按照官方的说法,JSHint 是一个社区驱动(community-driven)的工具,用于检测Ja ...

  9. 如何使用Apache的ab工具进行网站性能测试

    1.打开Apache服务器的安装路径,在bin目录中有一个ab.exe的可执行程序,就是我们要介绍的压力测试工具. 2.在Windows系统的命令行下,进入ab.exe程序所在目录,执行ab.exe程 ...

  10. POI-HSSF and POI-XSSF - Java API To Access Microsoft Excel Format Files

    一.概述 HSSF和XSSF是apache开源项目POI中实现java面向Excel的两个接口.两者的区别在于,HSSF适用于Excel '97(-2007)文档,而XSSF适用于Excel 2007 ...