HACK#56 การโปรแกรม Google Web API ด้วย Java

การโปรแกรม Google Web API ในจาวานั้นสามารถทำใด้อย่างรวดเร็ว เนื่องจากมีไลบรารี่ให้มาพร้อมกับ Google Web API Developer’s Kit อยู่แล้ว
ต้อง ขอขอบคุณ Java Archive (JAR) ซึ่งมีมาให้พร้อมกับ Google Web API Developer’s Kit [ใน “The Google Web APIs Developer’s Kit”] ซึ่งใน googleapi.jar archive ได้รวมเอา com.google.soap.search ซึ่งเป็น wrapper ที่ดีมากมาให้ด้วย พร้อมทั้ง Crimson XML parser (http://xml.apache.org/crimson) ของ Apache Software Foundation และตัวสุดท้ายที่มาพร้อมกันก็คือ Apache SOAP stack (http://xml.apache.org/soap)
  • Tip:คุณอาจจำเป็นต้องใช้ copy ของ Java 2 Platform, Standard Edition (J2SE, http://java.sun.com/downloads/) ในการคอมไพล์และรันการแฮ็กด้วยวิธีนี้
โค้ดตัวอย่าง
// Googly.java
// Bring in the Google SOAP wrapper
import com.google.soap.search.*;
import java.io.*;
public class Googly {
// Your Google API developer's key
private static String googleKey = "insert key here";
public static void main(String[] args) {
// Make sure there's a Google query on the command-line
if (args.length != 1) {
System.err.println("Usage: java [-classpath classpath] Googly ");
System.exit(1);
}
// Create a new GoogleSearch object
GoogleSearch s = new GoogleSearch( );
try {
s.setKey(googleKey);
s.setQueryString(args[0]); // Google query from the command-line
s.setMaxResults(10);
// Query Google
GoogleSearchResult r = s.doSearch( );
// Gather the results
GoogleSearchResultElement[] re = r.getResultElements( );
// Output
for ( int i = 0; i <>
System.out.println(re[i].getTitle( ));
System.out.println(re[i].getURL( ));
System.out.println(re[i].getSnippet( ) + "\n");
}
// Anything go wrong?
} catch (GoogleSearchFault f) {
System.out.println("GoogleSearchFault: " + f.toString( ));
}
}
}

โปรดอย่าลืมใส่ Google Developer Key (เช่น 12BuCK13mY5h0E/34KNocK@ttH3DoOR) ของคุณลงไปแทนใน “insert key here”
//Your Google API developer’s key
private static String googleKey = “12BuCK13mY5h0E/34KNocK@ttH3DoOR”;
การคอมไพล์โค้ด
สำหรับ การคอมไพล์ Googly application (Googly.java) คุณจำเป็นต้องมี googleapi.jar archive ก่อน สำหรับผู้เขียนได้เก็บเอาไว้ในไดเรกทอรีเดียวกับ googly.java แต่ถ้าหากคุณเก็บไว้ในที่อื่น ให้เปลี่ยนพาธหลัง –classpath ไปตามนั้น
% javac -classpath googleapi.jar Googly.java
ด้วยวิธีนี้คุณจะได้ไฟล์ Googly.class ใหม่ ซึ่งพร้อมที่จะทำงานได้แล้ว
Running the Hack
รัน Googly ที่ command line โดยการใส่คำถามที่ต้องการลงไปแทนที่ “query word”
% java -classpath .:googleapi.jar Googly "query words"
ผลลัพธ์
% java -classpath .:googleapi.jar Googly "Learning Java"
oreilly.com -- Online Catalog: Learning Java
http://www.oreilly.com/catalog/learnjava/
For programmers either just migrating to Java or already working
steadily in the forefront of Java development, Learning Java gives
a clear, systematic ...
oreilly.com -- Online Catalog: Learning Java , 2nd Edition
http://www.oreilly.com/catalog/learnjava2/
This new edition of Learning Java has been expanded and updated for
Java 2 Standard Edition SDK 1.4. It comprehensively addresses ...
...
Java Programming...From the Grounds Up / Web Developer
http://www.webdeveloper.com/java/java_programming_grounds_up.html
... WebDeveloper.com. Java Programming... From the Grounds Up. by
Mark C. Reynolds ... Java Classes and Methods. Java utilizes the
basic object technology found in C++. ...

โพสต์ยอดนิยมจากบล็อกนี้

I miss you all กับ I miss all of you ต่างกันอย่างไร

ปัญหาและเฉลยวิชาธรรม นักธรรมชั้นตรี สอบในสนามหลวง วันอังคาร ที่ ๒๙ กันยายน พ.ศ.๒๕๕๒

ปัญหาและเฉลยวิชาอนุพุทธประวัติ นักธรรมชั้นโท สอบในสนามหลวง วันอาทิตย์ ที่ ๒๐ พฤศจิกายน พ.ศ. ๒๕๔๘