google-http-java-client å ¥é
Java 㧠HTTP éä¿¡ããã¨ãã®ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªãä½ã«ããããã¤ãæ©ãã®ã§ãããæè¿ google-http-java-client ãæ°ã«ãªã£ã¦ãã®ã§ã¡ãã£ã¨ä½¿ã£ã¦ã¿ã¾ããã æ±ç¨çã« HTTP éä¿¡ãã§ããã°ãããã¨ãããããªç¨éã«ã¯ã¡ããã©è¯ãããã§ãã
æ°å¹´åãããã¼ã¿çã RC çã¨ãã¦ã¯åå¨ãã¦ãã¾ããããæ£å¼ã«ãªãªã¼ã¹ãããã®ã¯ä»å¹´ã®ããã§ãã
google-http-java-client ã«ã¤ãã¦
- ããã¸ã§ã¯ããã¼ã : google-http-java-client - Google HTTP Client Library for Java - Google Project Hosting
Google ã«ãã£ã¦æ¸ããã Java ã® HTTP ã¯ã©ã¤ã¢ã³ãã©ã¤ãã©ãªã§ãã
HTTP ãã©ã³ã¹ãã¼ãã®æ½è±¡åãããã¦ãããå®éã® HTTP éä¿¡ãè¡ãä½å±¤ã®ã©ã¤ãã©ãªãé¸æã§ããã®ãç¹å¾´ã§ãã ä¾ãã° java.net.HttpURLConnection
ã使ã£ãããApache HTTP Client ã使ã£ããã§ãã¾ãã
ã¾ãããªã¯ã¨ã¹ããã¬ã¹ãã³ã¹ã®ã³ã³ãã³ãããã£ã® XML ã JSON ã®ãã¼ã¹ãã·ãªã¢ã©ã¤ãºãè¡ãæ©è½ãå«ã¾ãã¦ããã便å©ã§ãã
使ç¨ã§ããç°å¢ã¯ãJava 5 以éã® Java SE ç°å¢ã Java EE ç°å¢ãAndroid 1.5 以éãªã©ã§ãã
ãã£ããã¨ãã使ãæ¹
æºå
使ç¨ããããã«ã¯ JAR ãã¡ã¤ã«ãã©ã¤ãã©ãªãã¹ã«è¿½å ããã¨ããMaven ã®ä¾åé¢ä¿ç®¡çã«è¿½å ããã¨ãããå¿ è¦ãããã¾ãã 2014 å¹´ 11 æ 8 æ¥ç¾å¨ã®ææ°ãã¼ã¸ã§ã³ã¯ 1.19.0 ã§ãMaven Central Repository ã«ç½®ããã¦ãã¾ãã
Gradle ã使ã£ã¦ãããªãã°æ¬¡ã®ããã«ä¾åé¢ä¿ãè¨è¿°ããã°ããã§ãã
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.http-client:google-http-client:1.19.0'
}
HTTP ãªã¯ã¨ã¹ããè¡ãå¦çã®å ¨ä½ã®æµã
HttpTransport
ãªãã¸ã§ã¯ããç¨æããã- ããã¯ã¢ããªã±ã¼ã·ã§ã³å ¨ä½ã§ 1 ã¤ã ãåå¨ããã°ããã
HttpTransport
ã®createRequestFactory
ã¡ã½ãããå¼ãã§HttpRequestFactory
ãªãã¸ã§ã¯ããçæãããHttpRequestFactory
ããHttpRequest
ãçæãããHttpRequest
ã®execute
ã¡ã½ãããå¼ã³åºãã¦ãªã¯ã¨ã¹ãå®è¡ãã¬ã¹ãã³ã¹ã¨ãã¦HttpResponse
ãåå¾ã- ã¬ã¹ãã³ã¹ãå¦çãããã¨ãçµäºå¦çã
ãµã³ãã«ã³ã¼ã
GET ãªã¯ã¨ã¹ããæãã¦ã¬ã¹ãã³ã¹ããã£ã表示ããä¾ã Gist ã«ç½®ãã¦ããã¾ãã ãã¼ã¸ã§ã³ 1.19.0 ã使ç¨ãã¦ãã¾ãã
- ãµã³ãã«ã³ã¼ã: google-http-java-client を使用するサンプルコード
ã¬ã¹ãã³ã¹ã®ãã¼ã¹
HttpRequest#setParser
ã¡ã½ãã ã使ã£ã¦ ObjectParser
ã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£
ããã¤ã³ã¹ã¿ã³ã¹ããã¼ãµã¼ã¨ãã¦ç»é²ãã¦ããã¨ãã¬ã¹ãã³ã¹ã®ãã¼ã¹ãä»»ãããã¨ãã§ãã¾ãã
ã©ã¤ãã©ãªã«å«ã¾ãã¦ãã ObjectParser
ã®å®è£
ã¨ãã¦ã¯ãJsonObjectParser
ã¯ã©ã¹ ã UrlEncodedParser
ã¯ã©ã¹ ãããã¾ãã
ãµã³ãã«ã³ã¼ã
/* å¿ è¦ãª import æ import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpResponse; import com.google.api.client.http.UrlEncodedParser; import com.google.api.client.util.GenericData; */ // HttpRequest ãªãã¸ã§ã¯ãã«ãã¼ãµã¼ãè¨å®ãã¦ããã // (req 㯠HttpRequest ãªãã¸ã§ã¯ã) req.setParser(new UrlEncodedParser()); // ãªã¯ã¨ã¹ãå®è¡ã HttpResponse res = req.execute(); try { // ã¬ã¹ãã³ã¹ã®ãã¼ã¹ãè¡ãã (ä¸ã§è¨å®ãã UrlEncodedParser ã使ãããã) GenericData d = res.parseAs(GenericData.class); } finally { // (以ä¸ç¥)
ã¬ã¹ãã³ã¹ã®ãã¼ã¹å¾ã®ã¯ã©ã¹ãä½ã
ä¸ã®ä¾ã§ã¯ãã¼ã¹å¾ã®ã¯ã©ã¹ã¨ã㦠GenericData
ã使ç¨ãã¾ããããã©ããããã©ã¡ã¼ã¿ã渡ã£ã¦ãããããã£ã¦ããå ´åããããåãåãããã®ã¯ã©ã¹ãç¨æãã¦ããã¦ããã£ã¼ã«ãã«å¤ãè¨å®ããããã¨ãã§ãã¾ãã
ä¾ãã°ãOAuth 1.0 Protocol ã® Temporary Credentials ãåå¾ããããã®ãªã¯ã¨ã¹ãã®ã¬ã¹ãã³ã¹ããã¼ã¹ããå ´åãèãã¦ã¿ã¾ãããã ã¬ã¹ãã³ã¹ã®å½¢å¼ã¯ ãoauth_token=xxxxx&oauth_token_secret=xxxxx&oauth_callback_confirmed=trueã ã¨ãããã®ã§ãããã¨ã¯ããã£ã¦ããã®ã§ã次ã®ãããªã¯ã©ã¹ã§ã¬ã¹ãã³ã¹ãåãåããã¨ãã§ãã¾ãã @Key
ã¢ããã¼ã·ã§ã³ ããã£ã¼ã«ãã«ä»ãããã¨ã§ããã¼ã¹ããçµæãåãåããã£ã¼ã«ãã§ãããã¨ã示ãã¦ãã¾ãã
/* å¿ è¦ãª import æ import com.google.api.client.util.GenericData; import com.google.api.client.util.Key; */ // å¿ ããã GenericData ãç¶æ¿ããå¿ è¦ã¯ãªãããç¶æ¿ãã¦ããã°ãã£ã¼ã«ãã§å®ç¾©ããã¦ããªããã©ã¡ã¼ã¿ãåãåããã¨ãã§ããã public class OAuthTemporaryCredentialResponse extends GenericData { @Key("oauth_token") public String identifier; @Key("oauth_token_secret") public String sharedSecret; @Key("oauth_callback_confirmed") public String callbackConfirmed; }
ããã¦ããã¼ã¹æã«ãã®ã¯ã©ã¹ãæå®ãããã¨ã§ãã¼ã¹çµæã OAuthTemporaryCredentialResponse
ãªãã¸ã§ã¯ãã¨ãã¦åãåããã¨ãã§ãã¾ãã
OAuthTemporaryCredentialResponse d = res.parseAs(OAuthTemporaryCredentialResponse.class); System.out.println("identifier: " + d.identifier); // ãã£ã¼ã«ãã¢ã¯ã»ã¹ã«ããã¬ã¹ãã³ã¹ã®å¤ã«ã¢ã¯ã»ã¹ã§ããã
é¢é£
- JavaでのHttpClientサンプル - Starlight : ä»ã® Java ã® HTTP ã¯ã©ã¤ã¢ã³ãã®ä½¿ç¨ä¾ãªã©ã
- JavaでのHttpClientサンプル その2 - Starlight : ä¸ã®ã¨ã³ããªã®ç¶ç·¨ã google-http-java-client ãå«ãããã¤ãã® HTTP ã¯ã©ã¤ã¢ã³ãã®ä½¿ç¨ä¾ãæ¸ããã¦ããã
- http://tree-tips.appspot.com/java/googleHttpJavaClient/ : google-http-java-client ã使ã£ã¦ XML ã JSON ã®ãã¼ã¹ããä¾ã