Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 圖文數據JSON解析,金山詞霸每日一句API的調用

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

編輯:關於Android編程

 

數據格式為

 
{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字段解釋

 
JSON 字段解釋
{
'sid':'' #每日一句ID
'tts': '' #音頻地址
'content':'' #英文內容
'note': '' #中文內容
'translation':'' #詞霸小編
'picture': '' #圖片地址
'picture2': '' #大圖片地址
'caption':'' #標題
'dateline':'' #時間
's_pv':'' #浏覽數
'sp_pv':'' #語音評測浏覽數
'tags':'' #相關標簽
'fenxiang_img':'' #合成圖片,建議分享微博用的
}
 

最終實現的效果
data-cke-saved-src=https://www.android5.online/Android/UploadFiles_5356/201702/2017022316102521.jpg

具體實現,使用AsynTask異步訪問網絡:

 
class Load extends AsyncTask
    {
        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

使用了一個圖片處理的工具類,ImageLoader,主要用來通過url解析圖片,處理圖片的大小,以文件的形式緩存圖片。

public class ImageLoader {
    
    MemoryCache memoryCache=new MemoryCache();
    FileCache fileCache;
    private Map imageViews=Collections.synchronizedMap(new WeakHashMap());
    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

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved