PowerWord development of the free API http://open.iciba.com/dsapi/
Data format is
{"Sid": "737","TTS": "Http:\/\/news.iciba.com\/admin\/tts\/2013-12-11.mp3","Content": "I don ' t want us to being together because we have to,i want us-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\[email protected]\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": "+", "name": "\u7535\u5f71\u7ecf\u5178"}],"Fenxiang_img": "Http:\/\/cdn.iciba.com\/web\/news\/longweibo\/imag\/2013-12-11.jpg"}
JSON field explanation
The JSON Field explains {' sid ': ' #每日一句ID' TTS ': ' #音频地址' content ': ' #英文内容' note ': ' #中文内容 ' translation ': ' #词霸小编' picture ': ' #图片地址' picture2 ': ' #大图片地址' caption ': ' #标题 ' Dateline ': ' #时间' s_pv ': ' #浏览数' sp_pv ': ' #语音评测浏览数' tags ': ' #相关标签 ' fenxiang_img ': ' #合成图片, suggested to share micro-blog
The effect of the final implementation
Specifically, use Asyntask to access the network asynchronously:
classLoadextendsAsynctask<string, String, string> { PublicString url = "http://open.iciba.com/dsapi/"; ProgressDialog Pdlg; String Jsonstr= ""; Jsonobject JSON=NULL; @Overrideprotectedstring Doinbackground (String ... params) {//TODO auto-generated Method Stub Try{defaulthttpclient httpClient=Newdefaulthttpclient (); HttpPost HttpPost=Newhttppost (URL); HttpResponse HttpResponse=Httpclient.execute (HttpPost); Httpentity httpentity=httpresponse.getentity (); InputStream is=httpentity.getcontent (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (IS, "UTF-8")); StringBuilder SB=NewStringBuilder (); String Line=NULL; while(line = Reader.readline ())! =NULL) {sb.append ( line+ "\ n"); } is.close (); Jsonstr=sb.tostring (); JSON=NewJsonobject (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(intI=0;i<array.length (); i++) {jsonobject tag=(jsonobject) array.get (i); Tagstr+ = tag.getstring ("name") + "," ; } }Catch(Exception e) {e.printstacktrace (); } return NULL; } @Overrideprotected voidOnPostExecute (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); } @Overrideprotected voidOnPreExecute () {//TODO auto-generated Method Stub Super. OnPreExecute (); Pdlg=NewProgressDialog (context); Pdlg.setcancelable (false); Pdlg.setmessage ("Loading"); Pdlg.show (); } }
Use a picture processing tool class, Imageloader, mainly used to parse the image through the URL, processing the size of the picture, as a file cache image.
Public classImageloader {memorycache memorycache=NewMemoryCache (); Filecache Filecache; PrivateMap<imageview, String> Imageviews=collections.synchronizedmap (NewWeakhashmap<imageview, string>()); Executorservice Executorservice; PublicImageloader (Context context) {Filecache=NewFilecache (context); Executorservice=executors.newfixedthreadpool (5); } Final intstub_id =R.drawable.drug_trans; Public voiddisplayimage (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 voidqueuephoto (String URL, ImageView ImageView) {phototoload P=Newphototoload (URL, imageView); Executorservice.submit (NewPhotosloader (p)); } PrivateBitmap getbitmap (String URL) {File f=filecache.getfile (URL); //From SD CacheBitmap B =DecodeFile (f); if(b!=NULL) returnb; //From Web Try{Bitmap Bitmap=NULL; URL IMAGEURL=Newurl (URL); HttpURLConnection Conn=(HttpURLConnection) imageurl.openconnection (); Conn.setconnecttimeout (30000); Conn.setreadtimeout (30000); Conn.setinstancefollowredirects (true); InputStream is=Conn.getinputstream (); OutputStream OS=NewFileOutputStream (f); Utils.copystream (is, OS); Os.close (); Bitmap=DecodeFile (f); returnbitmap; } Catch(Exception ex) {ex.printstacktrace (); return NULL; } } //decodes image and scales it to reduce memory consumption PrivateBitmap DecodeFile (File f) {Try { //decode Image sizeBitmapfactory.options o =Newbitmapfactory.options (); O.injustdecodebounds=true; Bitmapfactory.decodestream (NewFileInputStream (f),NULL, O); //Find the correct scale value. It should be the power of 2. Final intRequired_size=70; intWidth_tmp=o.outwidth, height_tmp=O.outheight; intScale=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 InsamplesizeBitmapfactory.options O2 =Newbitmapfactory.options (); O2.insamplesize=Scale ; returnBitmapfactory.decodestream (NewFileInputStream (f),NULL, O2); } Catch(FileNotFoundException e) {}return NULL; } //Task for the queue Private classPhototoload { PublicString URL; PublicImageView ImageView; PublicPhototoload (String u, ImageView i) {URL=u; ImageView=i; } } classPhotosloaderImplementsRunnable {phototoload phototoload; Photosloader (Phototoload phototoload) { This. phototoload=Phototoload; } @Override Public voidrun () {if(imageviewreused (phototoload))return; Bitmap BMP=Getbitmap (Phototoload.url); Memorycache.put (Phototoload.url, BMP); if(imageviewreused (phototoload))return; Bitmapdisplayer BD=NewBitmapdisplayer (BMP, Phototoload); Activity a=(Activity) photoToLoad.imageView.getContext (); A.runonuithread (BD); } } Booleanimageviewreused (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 classBitmapdisplayerImplementsRunnable {Bitmap Bitmap; Phototoload Phototoload; PublicBitmapdisplayer (Bitmap B, Phototoload p) {bitmap=b;phototoload=p;} Public voidrun () {if(imageviewreused (phototoload))return; if(bitmap!=NULL) PhotoToLoad.imageView.setImageBitmap (bitmap); ElsePhotoToLoad.imageView.setImageResource (stub_id); } } Public voidClearCache () {memorycache.clear (); Filecache.clear (); }}
Android Graphics Data JSON parsing, PowerWord daily an API call