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

数据格式为

 

1
2
3
4
5
6
7
8
9
10
11
{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:我不希望我们因为“不得不”而在一起,我希望我们是因为想在一起而在一起。,
translation:感谢@程很多要秒虐数学 投稿。词霸小编,这句话来自《冰河世纪2》,是一个系列的动画电影,非常搞笑,你看过吗?,
picture:http://cdn.iciba.com/news/word/2013-12-11.jpg,picture2:http://cdn.iciba.com/news/word/big_2013-12-11b.jpg,caption:词霸每日一句,
dateline:2013-12-11,
s_pv:8693,
sp_pv:2090,
tags:[{id:9,name:爱情},{id:14,name:电影经典}],
fenxiang_img:http://cdn.iciba.com/web/news/longweibo/imag/2013-12-11.jpg}

 

 

JSON字段解释

 

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

 

最终实现的效果
技术分享

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class Load extends AsyncTask<string, string="">
    {
        public String url = http://open.iciba.com/dsapi/;
        ProgressDialog pdlg;
        String jsonstr = ;
        JSONObject json = null;
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            try{
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpResponse httpResponse = httpClient.execute(httppost);
                HttpEntity httpEntity = httpResponse.getEntity();
                InputStream is = httpEntity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, UTF-8));
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line +
);
                }
                is.close();
                jsonstr = sb.toString();
                json = new JSONObject(jsonstr.toString());
                engstr = json.getString(content);
                chistr = json.getString(note);
                imagurl = json.getString(picture);
                timestr =    json.getString(dateline);
                fromstr   =  json.getString(translation);
                JSONArray array = json.getJSONArray(tags);
                for(int i=0;i<p>使用了一个图片处理的工具类,ImageLoader,主要用来通过url解析图片,处理图片的大小,以文件的形式缓存图片。</p><pre class="brush:java;">public class ImageLoader {
     
    MemoryCache memoryCache=new MemoryCache();
    FileCache fileCache;
    private Map<imageview, string=""> imageViews=Collections.synchronizedMap(new WeakHashMap<imageview, string="">());
    ExecutorService executorService;
     
    public ImageLoader(Context context){
        fileCache=new FileCache(context);
        executorService=Executors.newFixedThreadPool(5);
    }
     
    final int stub_id = R.drawable.drug_trans;
    public void DisplayImage(String url, ImageView imageView)
    {
        imageViews.put(imageView, url);
        Bitmap bitmap=memoryCache.get(url);
        if(bitmap!=null)
            imageView.setImageBitmap(bitmap);
        else
        {
            queuePhoto(url, imageView);
            imageView.setImageResource(stub_id);
        }
    }
         
    private void queuePhoto(String url, ImageView imageView)
    {
        PhotoToLoad p=new PhotoToLoad(url, imageView);
        executorService.submit(new PhotosLoader(p));
    }
     
    private Bitmap getBitmap(String url)
    {
        File f=fileCache.getFile(url);
         
        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)
            return b;
         
        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }
    }
 
    //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);
             
            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=70;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/1.5<required_size a="(Activity)photoToLoad.imageView.getContext();" activity="" bd="new" bitmap="b;photoToLoad=p;}" bitmapdisplayer="" bitmapfactory.options="" bmp="getBitmap(photoToLoad.url);" boolean="" catch="" decode="" display="" else="" filenotfoundexception="" for="" height_tmp="" imageview="" implements="" in="" insamplesize="" new="" o2="new" o2.insamplesize="scale;" override="" photosloader="" phototoload="" pre="" private="" public="" queue="" return="" runnable="" string="" tag="=null" task="" the="" this.phototoload="photoToLoad;" thread="" to="" ui="" url="u;" used="" void="" width_tmp="" with=""><p> </p><p> </p></required_size></imageview,></imageview,></pre>
</array.length();i&#43;&#43;)></string,>
 

推推族,免费得门票,游景区:www.tuituizu.com

结伴旅游,一个免费的交友网站:www.jieberu.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。