Skip to content

Instantly share code, notes, and snippets.

@totem3
Created December 3, 2011 14:06
Show Gist options
  • Save totem3/1427207 to your computer and use it in GitHub Desktop.
Save totem3/1427207 to your computer and use it in GitHub Desktop.
GetのResponseどうやって受け取れば。。(´・ω・`)
import java.io._
import java.net._
import sun.net.www.protocol.http.HttpURLConnection
val url = new URL("http://api.tabelog.com/Ver2.1/RestaurantSearch/?key=value")
val http = url.openConnection.asInstanceOf[HttpURLConnection]
http.setRequestMethod("GET")
http.connect
val bis = new BufferedInputStream(http.getInputStream)
val b = new Array[Byte](bis.available)
bis.read(b)
val baos = ByteArrayOutputStream
baos.write(b)
val res = baos.toString("utf-8")
@xuwei-k
Copy link

xuwei-k commented Dec 3, 2011

GET で受け取るのが、(byte列ではなく) 単に文字列なら、標準ライブラリのみで

import scala.io.Source

Source.fromURL("http://search.twitter.com/search.json?q=scala").mkString

だけでできますよ。POSTリクエストだったり、色々やろうとすると(標準ライブラリだけでもできなくはないが)
なにか違うライブラリ使ったほうが楽ですが

@totem3
Copy link
Author

totem3 commented Dec 3, 2011

あ、、標準ライブラリにもそんな便利なものがありましたか。。。
勉強不足ですみません。
標準ライブラリ読みます(*´Д`)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment