Android圖文數據JSON解析,金山詞霸每日一句API的(de)調用 - 新聞資訊 - 雲南小程序開發|雲南軟件開發|雲南網站建設-昆明融晨信息技術有限公司

159-8711-8523

雲南網建設/小程序開發/軟件開發

知識

不(bù)管是(shì)網站,軟件還是(shì)小程序,都要(yào / yāo)直接或間接能爲(wéi / wèi)您産生價值,我們在(zài)追求其視覺表現的(de)同時(shí),更側重于(yú)功能的(de)便捷,營銷的(de)便利,運營的(de)高效,讓網站成爲(wéi / wèi)營銷工具,讓軟件能切實提升企業内部管理水平和(hé / huò)效率。優秀的(de)程序爲(wéi / wèi)後期升級提供便捷的(de)支持!

您當前位置>首頁 » 新聞資訊 » 技術分享 >

Android圖文數據JSON解析,金山詞霸每日一句API的(de)調用

發表時(shí)間:2020-12-6

發布人(rén):融晨科技

浏覽次數:145


金山詞霸開發的(de)免費API http://open.iciba.com/dsapi/
數據格式爲(wéi / wèi)
[img]http://common.cnblogs.com/images/copycode.gif
{"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":"我不(bù)希望我們\u56e0爲(wéi / wèi)\u201c不(bù)得不(bù)\u201d而(ér)在(zài)一起\uff0c我希望我們是(shì)\u56e0爲(wéi / wèi)想在(zài)一起而(ér)在(zài)一起\u3002",
"translation":"感謝@程很多要(yào / yāo)秒虐數學 投稿\u3002詞霸小編\uff0c這(zhè)句話來(lái)自\u300a冰河世紀2\u300b\uff0c是(shì)一個(gè)系列的(de)動畫電影\uff0c非常搞笑\uff0c你看過嗎\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":"詞霸每日一句",
"dateline":"2013-12-11",
"s_pv":"8693",
"sp_pv":"2090",
"tags":[{"id":"9","name":"愛情"},{"id":"14","name":"電影經典"}],
"fenxiang_img":"../static/picture/2013-12-11.jpg"}

[img]http://common.cnblogs.com/images/copycode.gif
JSON字段解釋
[img]http://common.cnblogs.com/images/copycode.gif
JSON 字段解釋
{
'sid':'' #每日一句ID
'tts': '' #音頻地(dì / de)址
'content':'' #英文内容
'note': '' #中文内容
'translation':'' #詞霸小編
'picture': '' #圖片地(dì / de)址
'picture2': '' #大(dà)圖片地(dì / de)址
'caption':'' #标題
'dateline':'' #時(shí)間
's_pv':'' #浏覽數
'sp_pv':'' #語音評測浏覽數
'tags':'' #相關标簽
'fenxiang_img':'' #合成圖片,建議分享微博用的(de)
}

[img]http://common.cnblogs.com/images/copycode.gif
最終實現的(de)效果
[img]http://images.cnitblog.com/blog/375789/201312/11195922-6894719bbc4a4ce6bc1f1bc168f289b7.jpg
具體實現,使用AsynTask異步訪問網絡:
[img]http://common.cnblogs.com/images/copycode.gif
class Load extends AsyncTask<String, 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 + "\n");
                }
                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<array.length();i++)
                {
                    JSONObject tag = (JSONObject)array.get(i);
                    tagstr += tag.getString("name")+"," ; 
                }
            }catch(Exception e)
            {
                e.printStackTrace();
            }
            
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            pdlg.dismiss();
            imageLoader.DisplayImage(imagurl,imageview);
            eng.setText("    "+engstr);
            chi.setText("    "+chistr);
            tag.setText(tagstr);
            time.setText(timestr);
            from.setText("    "+fromstr);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pdlg = new ProgressDialog(context);
            pdlg.setCancelable(false);
            pdlg.setMessage("正在(zài)加載");
            pdlg.show();
        }
    }

[img]http://common.cnblogs.com/images/copycode.gif
使用了(le/liǎo)一個(gè)圖片處理的(de)工具類,ImageLoader,主要(yào / yāo)用來(lái)通過url解析圖片,處理圖片的(de)大(dà)小,以(yǐ)文件的(de)形式緩存圖片。
[img]http://common.cnblogs.com/images/copycode.gif
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 || height_tmp/1.5<REQUIRED_SIZE)
                    break;
                width_tmp/=1.5;
                height_tmp/=1.5;
                scale*=1.5;
            }
            
            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }
    
    //Task for the queue
    private class PhotoToLoad
    {
        public String url;
        public ImageView imageView;
        public PhotoToLoad(String u, ImageView i){
            url=u; 
            imageView=i;
        }
    }
    
    class PhotosLoader implements Runnable {
        PhotoToLoad photoToLoad;
        PhotosLoader(PhotoToLoad photoToLoad){
            this.photoToLoad=photoToLoad;
        }
        
        @Override
        public void run() {
            if(imageViewReused(photoToLoad))
                return;
            Bitmap bmp=getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);
            if(imageViewReused(photoToLoad))
                return;
            BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
            Activity a=(Activity)photoToLoad.imageView.getContext();
            a.runOnUiThread(bd);
        }
    }
    
    boolean imageViewReused(PhotoToLoad photoToLoad){
        String tag=imageViews.get(photoToLoad.imageView);
        if(tag==null || !tag.equals(photoToLoad.url))
            return true;
        return false;
    }
    
    //Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable
    {
        Bitmap bitmap;
        PhotoToLoad photoToLoad;
        public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
        public void run()
        {
            if(imageViewReused(photoToLoad))
                return;
            if(bitmap!=null)
                photoToLoad.imageView.setImageBitmap(bitmap);
            else
                photoToLoad.imageView.setImageResource(stub_id);
        }
    }

    public void clearCache() {
        memoryCache.clear();
        fileCache.clear();
    }

}

[img]http://common.cnblogs.com/images/copycode.gif
其他(tā)精彩文章文章

jQuery教程(10)-DOM樹操作之(zhī)内容setter和(hé / huò)getter方法

android學習筆記(37)使用 DatePickerDialog、TimePickerDialog


android學習筆記(36)使用AlertDialog創建自定義對話框


jQuery教程(1)-操作DOM之(zhī)操作屬性

Spring mvc新手入門(11)-返回json 字符串的(de)其他(tā)方式


更多關于(yú)android開發文章

相關案例查看更多