0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

IntelliJ: Java 標準ライブラリーで HTTP リクエスト

Posted at

次のサンプルの
Java 標準ライブラリの API を使用して Kotlin で HTTP リクエストを作成する
と同じことを行いました。
Kotlin で HTTP リクエストを作成する

image.png

サーバー仕様

次のようにして、JSON が GET で取得できるとします。

http https://ekzemplaro.org/tmp/country_all.json

取得できる JSON

country_all.json
[{"id":18,"countryName":"Kenya"},
{"id":20,"countryName":"Tanzania"},
{"id":21,"countryName":"Ethiopia"},
{"id":22,"countryName":"Malawi"},
{"id":23,"countryName":"Country"},
{"id":24,"countryName":"Country"},
{"id":25,"countryName":"Kenya"},
{"id":26,"countryName":"Country"},
{"id":27,"countryName":"USA"},
{"id":28,"countryName":"USA"},
{"id":29,"countryName":"Kenya"},
{"id":30,"countryName":"Kenya"}]

プログラム

Main.kt
import java.net.HttpURLConnection
import java.net.URI

fun main(){

    val url_string = "https://ekzemplaro.org/tmp/country_all.json"
    val uri =  URI(url_string)
    val url = uri.toURL()
    val con = url.openConnection() as HttpURLConnection

    // 接続設定(ミリ秒で指定)
    con.connectTimeout = 20_000 // 20 秒
    con.readTimeout = 20_000    // 20 秒
    con.requestMethod = "GET"   // GETの場合は省略可能

    // 接続を確立
    con.connect()

    // レスポンスを取得
    val str = con.inputStream.bufferedReader(Charsets.UTF_8).use { br ->
        br.readLines().joinToString("")
    }

    // 表示
    println(str)
}

実行結果

/usr/lib/jvm/java-1.21.0-openjdk-amd64/bin/java -javaagent:/snap/intellij-idea-community/464/lib/idea_rt.jar=36781:/snap/intellij-idea-community/464/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath /home/uchida/IdeaProjects/get01/build/classes/kotlin/main:/home/uchida/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk8/1.9.0/e000bd084353d84c9e888f6fb341dc1f5b79d948/kotlin-stdlib-jdk8-1.9.0.jar:/home/uchida/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-jdk7/1.9.0/f320478990d05e0cfaadd74f9619fd6027adbf37/kotlin-stdlib-jdk7-1.9.0.jar:/home/uchida/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.9.0/8ee15ef0c67dc83d874f412d84378d7f0eb50b63/kotlin-stdlib-1.9.0.jar:/home/uchida/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.9.0/cd65c21cfd1eec4d44ef09f9f52b6d9f8a720636/kotlin-stdlib-common-1.9.0.jar:/home/uchida/.gradle/caches/modules-2/files-2.1/org.jetbrains/annotations/13.0/919f0dfe192fb4e063e7dacadee7f8bb9a2672a9/annotations-13.0.jar MainKt
[{"id":18,"countryName":"Kenya"},{"id":20,"countryName":"Tanzania"},{"id":21,"countryName":"Ethiopia"},{"id":22,"countryName":"Malawi"},{"id":23,"countryName":"Country"},{"id":24,"countryName":"Country"},{"id":25,"countryName":"Kenya"},{"id":26,"countryName":"Country"},{"id":27,"countryName":"USA"},{"id":28,"countryName":"USA"},{"id":29,"countryName":"Kenya"},{"id":30,"countryName":"Kenya"}]

Process finished with exit code 0
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?