First try Google and Flickr Image Source

Source: Internet
Author: User
Package COM. jadyer. util; import Java. io. bufferedreader; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import java.net. malformedurlexception; import java.net. URL; import Java. util. arraylist; import Java. util. list; /*** @ see ========================================== ========================================================== ================================================================= *@ see find Google Image Search On the API page * @ see 1, go to the 'HTTP: // code.google.com 'website, click Ajax APIs on the left, and the page will jump to 'https: // developers.google.com/loader /? Hl = ZH-CN '* @ see 2. Expand the left-side APIs using the loader and Click image search to jump to 'https: // developers.google.com/image-search /? Hl = ZH-CN '* @ see 3. Click the developer documentation link on the page to jump to 'https: // developers.google.com/image-search/v1/index? Hl = ZH-CN '* @ see 4. Click the JSON developer's guide link on the page to jump to 'https: // developers.google.com/image-search/v1/jsondevguide? Hl = ZH-CN '* @ see ================================ ========================================================== ========================================================== = * @ see method for parsing the JSON string returned by Google * @ see in the JSON string returned by Google, the URL of the image is directly added to JSON with the 'url' parameter and returned to us * @ see. Therefore, we can directly parse the 'url' parameter value in the returned JSON string. The following is an example of the format of the JSON string returned by Google * @ see {"responsedata": {"Results": [{"gsearchresultclass": "gimagesearch", "width": "220 ", "Height": "204", * @ see "imageid": "ANd9GcSvKWlLJwv3gayNuFIpl0eCPwIcdEwUzdFo-HsTZv-z1l5JL7WYMhacZg", "tbwidth": "107", "tbheight": "99 ", * @ see "unescapedurl": "http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/IBM_Thinkpad_R51.jpg/220px-IBM_Thinkpad_R51.jpg", * @ see "url": "http: // upload. Principal ", * @ see" visibleurl ":" en.wikipedia.org "," title ":" \ u003cb \ u003ethinkpad \ u003c/B \ u003e-Wikipedia, the free encyclopedia ", * @ see "titlenoformatting": "ThinkPad-Wikipedia, the free encyclopedia", "originalcontexturl": "http://en.wikipedia.org/wiki/ThinkPad", * @ see "content ": "IBM \ u003cb \ u003ethinkpad \ u003c/B \ U003e R51 "," contentnoformatting ":" IBM ThinkPad R51 ", * @ see" tburl ":" http://t3.gstatic.com/images? Q \ u003dtbn: ANd9GcSvKWlLJwv3gayNuFIpl0eCPwIcdEwUzdFo-HsTZv-z1l5JL7WYMhacZg "}, * @ see {......}, * @ see "responsedetails": NULL, "responsestatus ": 200} * @ see so the image URL we get is the value of the 27th line 'url' parameter http://upload.wikimedia.org/wikipedia/commons/thumb/0/0e/IBM_Thinkpad_R51.jpg/220px-IBM_Thinkpad_R51.jpg * @ see ================ ========================================================== ========================================================== ============ =======* @ See find the QR code. photos. on the search API page * @ see 1, click the 'api description file' link at the bottom of the 'HTTP: // www.flickr.com 'homepage on the Flickr homepage to go to 'HTTP: // www.flickr.com/services/api/' * @ see 2. Click 'flickr. photos. the 'search' link jumps to 'HTTP: // www.flickr.com/services/api/flickr.photos.search.html' * @ see 3. There is also an 'HTTP: // required * @ see 4. If you want to view the JSON format returned by Flickr, click 'response format' on the left of the 'api description file' page. The 'json' link * @ see its specific page is 'HTTP: // www.flickr.com/services/api/response.json.html' * @ see ========================== ========================================================== ================================================================= *@ how does see parse the JSON string returned by Flickr * @ see 0, unlike Google, google will directly add the image URL to the returned JSON, and Flickr needs to parse the returned JSON, And Then assemble the image URL * @ see 1. Click the 'HTTP: // The 'api description file' link at the bottom of www.flickr.com will be redirected to 'HTTP: // www.flickr.com/s Ervices/API/'* @ see 2. Click the 'url' link on the left side of the page to jump to 'HTTP: // www.flickr.com/services/api/misc.urls.html' * @ see 3. Read this page and find that the URL of the image is spliced by yourself, such as http: // farm {farm-ID} .staticflickr.com/{server-id}/{id}_{secret}.jpg * @ see 4. The following is the sample format of the JSON string returned by Flickr * @ see jsonflickrapi ({"Photos": {"page": 1, "pages": 429, "perpage": 300, "Total": "128631", * @ see "photo": [{"ID": "6845488017 ", "owner": "71906482 @ n04", "secret": "0f4c482 CED "," server ":" 7007 "," Farm ": 8, * @ see" title ":" Mini-mini schnauzers "," ispublic ": 1, "isfriend": 0, "isfamily": 0}, * @ see {...}]}, * @ see "stat": "OK"}) * @ see 5. the URL of the image spliced Based on the JSON string is 'HTTP: // farm8.staticflickr.com/7007/6845488017_0f4c482ced.jpg' * @ see ============================================ ========================================================== ================================================================= *@ author macro Yu * @ create Feb 9, 2012 6:25:38 */public class imagesearchapisutil {public static void main (string [] ARGs) {// string googlebaseurl = "https://ajax.googleapis.com/ajax/services/search/images"; // string googleimgsize = "large "; // string googlesearchcontent = "ThinkPad"; // getgoogleimage (googlebaseurl, googleimgsize, googlesearchcontent); string flickrbaseurl = "http://api.flickr.com/services/rest/"; string flickrmetho D = "Flickr. Photos. Search"; string flickrapikey = "da8e8c6840579831969d1a87d37ae545"; string flickrperpage = "200"; // set the number of images returned for each page. The default value is 100, and the maximum value is 500 string flickrformat = "JSON"; // specify that the returned data is in JSON Format String flickrtags = "MacBook "; // keyword of the image to be queried: getflickrimage );} /*** get the Google image source * @ Param googlebaseurl Google image server URL * @ Param googleimgsize the size of the image to be displayed * @ Param googlesearchcontent the keyword to be queried * @ see for the same keyword, google only returns 64 images, and returns a maximum of 8 images at a time * @ see. So we can loop through 8 times to get these 64 images. Images, parse the URLs of these 64 images, and send these URLs to the client in response */public static void getgoogleimage (string googlebaseurl, string googleimgsize, string googlesearchcontent) {list <string> List = new arraylist <string> (); For (INT I = 0; I <8; I ++) {string url = googlebaseurl + "? Q = "+ googlesearchcontent +" & rsz = "+ googleimgsize +" & V = 1.0 & "; Url + =" Start = "+ 8 * I; stringbuffer sb = new stringbuffer (); inputstream is = NULL; inputstreamreader ISR = NULL; bufferedreader BR = NULL; try {is = new URL (URL ). openconnection (). getinputstream (); // get input stream ISR = new inputstreamreader (is, "UTF-8"); // convert byte stream to bytes stream BR = new bufferedreader (ISR); string line = NULL; while (null! = (Line = BR. readline () {sb. append (line);} List. add (sb. tostring ();} catch (malformedurlexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {If (null! = Br) {try {Br. Close ();} catch (ioexception e) {e. printstacktrace () ;}} if (null! = ISR) {try {ISR. Close () ;}catch (ioexception e) {e. printstacktrace () ;}} if (null! = Is) {try {is. close ();} catch (ioexception e) {e. printstacktrace () ;}}} system. out. println ("number of images returned by Google:" + list. size (); For (string S: List) {system. out. println (s );}} /*** obtain the image source of the Flickr image * @ Param flickrbaseurl: the URL of the Flickr image server * @ Param flickrmethod: The image search method provided by the Flickr * @ Param flickrapikey: the free api_key provided by the flickrperpage set the number of images returned on each page * @ Param flickrformat specifies the format of the returned data * @ Param flickrtags query the image keyword * @ See I applied for Yahoo Mail address is mytestflickr32@yahoo.com password is uygnoh * @ see I applied for the Flickr API key for fuse, its password is 243a1b4492339c84 */public static void getflickrimage (string flickrbaseurl, string flickrmethod, string flickrapikey, string flickrperpage, string flickrformat, string flickrtags) {string url = flickrbaseurl + "? Method = "+ flickrmethod +" & api_key = "+ flickrapikey +" & per_page = "+ flickrperpage +" & format = "+ flickrformat +" & tags = "+ flickrtags + "& extras = original_format "; stringbuffer sb = new stringbuffer (); inputstream is = NULL; inputstreamreader ISR = NULL; bufferedreader BR = NULL; try {is = new URL (URL ). openconnection (). getinputstream (); ISR = new inputstreamreader (is, "UTF-8"); BR = new bufferedreader (ISR ); String line = NULL; while (null! = (Line = BR. readline () {sb. append (line);} system. out. println (SB);} catch (malformedurlexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} finally {If (null! = Br) {try {Br. Close ();} catch (ioexception e) {e. printstacktrace () ;}} if (null! = ISR) {try {ISR. Close () ;}catch (ioexception e) {e. printstacktrace () ;}} if (null! = Is) {try {is. Close () ;}catch (ioexception e) {e. printstacktrace ();}}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.