知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運(yùn)營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏壧峁┍憬莸闹С郑?
Android圖文數(shù)據(jù)JSON解析,金山詞霸每日一句API的調(diào)用
發(fā)表時間:2020-12-6
發(fā)布人:葵宇科技
瀏覽次數(shù):145
金山詞霸開發(fā)的免費(fèi)API http://open.iciba.com/dsapi/
數(shù)據(jù)格式為
[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":"\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"}
[img]http://common.cnblogs.com/images/copycode.gif
JSON字段解釋
[img]http://common.cnblogs.com/images/copycode.gif
JSON 字段解釋 { 'sid':'' #每日一句ID 'tts': '' #音頻地址 'content':'' #英文內(nèi)容 'note': '' #中文內(nèi)容 'translation':'' #詞霸小編 'picture': '' #圖片地址 'picture2': '' #大圖片地址 'caption':'' #標(biāo)題 'dateline':'' #時間 's_pv':'' #瀏覽數(shù) 'sp_pv':'' #語音評測瀏覽數(shù) 'tags':'' #相關(guān)標(biāo)簽 'fenxiang_img':'' #合成圖片,建議分享微博用的 }
[img]http://common.cnblogs.com/images/copycode.gif
最終實(shí)現(xiàn)的效果
[img]http://images.cnitblog.com/blog/375789/201312/11195922-6894719bbc4a4ce6bc1f1bc168f289b7.jpg
具體實(shí)現(xiàn),使用AsynTask異步訪問網(wǎng)絡(luò):
[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("正在加載"); pdlg.show(); } }
[img]http://common.cnblogs.com/images/copycode.gif
使用了一個圖片處理的工具類,ImageLoader,主要用來通過url解析圖片,處理圖片的大小,以文件的形式緩存圖片。
[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
其他精彩文章文章
jQuery教程(10)-DOM樹操作之內(nèi)容setter和getter方法
android學(xué)習(xí)筆記(37)使用 DatePickerDialog、TimePickerDialog
android學(xué)習(xí)筆記(36)使用AlertDialog創(chuàng)建自定義對話框
jQuery教程(1)-操作DOM之操作屬性
Spring mvc新手入門(11)-返回json 字符串的其他方式
更多關(guān)于android開發(fā)文章
相關(guān)案例查看更多
相關(guān)閱讀
- 網(wǎng)站建設(shè)服務(wù)公司
- 網(wǎng)站建設(shè)服務(wù)
- 微信分銷系統(tǒng)
- 英文網(wǎng)站建設(shè)公司
- 小程序生成海報
- 開發(fā)框架
- 云南省建設(shè)廳網(wǎng)站
- 昆明小程序開發(fā)聯(lián)系方式
- 支付寶小程序被騙
- 昆明小程序開發(fā)
- 云南網(wǎng)站建設(shè)公司地址
- 汽車報廢管理
- 報廢車管理系統(tǒng)
- web
- 網(wǎng)站建設(shè)費(fèi)用
- 海報插件
- 昆明軟件公司
- 開通微信小程序被騙
- 人人商城
- 報廢車管理
- 紅河小程序開發(fā)
- 云南網(wǎng)站建設(shè)列表網(wǎng)
- 前端技術(shù)
- 報廢車拆解管理系統(tǒng)
- 云南省城鄉(xiāng)建設(shè)廳網(wǎng)站
- 云南etc小程序
- 高端網(wǎng)站建設(shè)公司
- 做網(wǎng)站
- 報廢車
- 云南省建設(shè)廳網(wǎng)站官網(wǎng)