SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

Java開発者のためのCurl入門(AD)

Java開発者のためのCurl入門-クライアントサイドCurlとサーバサイドJavaの通信

第2回

  • このエントリーをはてなブックマークに追加

アプリケーションの作成 2

クライアントサイドの作成

 まずは、実行結果を見てください。

 JSONのAPIを利用するためには、CURL.IO.JSONパッケージのインポートが必要です。そして、JsonValue-parseプロシージャを使用してアクセス先のURLデータを受け取り、JsonValueオブジェクトへ変換します。

JSONデータの受信
{import * from CURL.IO.JSON}

|| サーバからのJSONデータの受信
{let empJsonValue: JsonValue =
    {JsonValue-parse
       {url "http://localhost:8080/curl_server/emp.json"}
    }
}

 実行結果のようなグリッドテーブル表示を行うには、RecordGridを使用します。

 RecordGridを使用するには、まず、各列に対する名前と型、それにcaptionで見出し(実行結果のヘッダー)を宣言します。

RecordGridの宣言
{let employees: RecordSet =
    {RecordSet
        {RecordFields
              {RecordField "empno", caption = "empno", domain = int}
            , {RecordField "ename", caption = "ename",  domain = String}
            , {RecordField "job", caption = "job",  domain = String}
            , {RecordField "mgr", caption = "mgr",  domain = int}
            , {RecordField "hiredate", caption = "hiredate",  domain = double}
            , {RecordField "sal", caption = "sal",  domain = double}
            , {RecordField "deptno", caption = "deptno",  domain = int}
        }
    }
}

 サーバから受信したJsonValueオブジェクトの値を、RecordSetにセットして最後に表示を行えば、先ほどの実行結果を表示できます。

JsonValueオブジェクトの値をRecordSetにセット
|| Jsonデータをcustomersに追加する
{define-proc public {add-rec
                        empno:int
                        , ename: String
                        , job: String
                        , mgr: int
                        , hiredate: double
                        , sal: double
                        , deptno: int
                    }: void
    let new-rec: Record = {employees.new-record}
    {new-rec.set "empno", empno}
    {new-rec.set "ename", ename}
    {new-rec.set "job", job}
    {new-rec.set "mgr", mgr}
    {new-rec.set "hiredate", hiredate}
    {new-rec.set "sal", sal}
    {new-rec.set "deptno", deptno}
    {employees.append new-rec}
}

{for emp: JsonObject in empJsonValue do
    let found?: bool = false
    let empno: int = 0
    let ename: String = {String}
    let job: String = {String}
    let mgr: int = 0
    let hiredate: double = 0
    let sal: double = 0
    let deptno: int = 0
    set (empno, found?) = {emp.get-if-exists "empno"}
    set (ename, found?) = {emp.get-if-exists "ename"}
    set (job, found?) = {emp.get-if-exists "job"}
    set (mgr, found?) = {emp.get-if-exists "mgr"}
    set (hiredate, found?) = {emp.get-if-exists "hiredate"}
    set (sal, found?) = {emp.get-if-exists "sal"}
    set (deptno, found?) = {emp.get-if-exists "deptno"}
    {if found? then
        {add-rec empno, ename, job, mgr, hiredate, sal, deptno}
    }
}
データを表示するコード
{value
    {RecordGrid
        width=700
        , record-source = employees
    }
}

 以上で、DBのデータをそのまま表示するというシンプルなアプリケーションを作成しました。非常に簡単なコーディングでグリッドテーブルの表示ができています。また、クライアントサイドでヘッダーをクリックすれば、ソートをすることも可能です。

 最後に、start.curlのコード全体を掲載します。

start.curlのコード全体
{curl 6.0 applet}
{curl-file-attributes character-encoding = "shift-jis"}

{import * from CURL.IO.JSON}

|| サーバからのJSONデータの受信
{let empJsonValue: JsonValue =
    {JsonValue-parse
       {url "http://localhost:8080/curl_server/emp.json"}
    }
}

{let employees: RecordSet =
    {RecordSet
        {RecordFields
              {RecordField "empno", caption = "empno", domain = int}
            , {RecordField "ename", caption = "ename",  domain = String}
            , {RecordField "job", caption = "job",  domain = String}
            , {RecordField "mgr", caption = "mgr",  domain = int}
            , {RecordField "hiredate", caption = "hiredate",  domain = double}
            , {RecordField "sal", caption = "sal",  domain = double}
            , {RecordField "deptno", caption = "deptno",  domain = int}
        }
    }
}

|| Jsonデータをcustomersに追加する
{define-proc public {add-rec
                        empno:int
                        , ename: String
                        , job: String
                        , mgr: int
                        , hiredate: double
                        , sal: double
                        , deptno: int
                    }: void
    let new-rec: Record = {employees.new-record}
    {new-rec.set "empno", empno}
    {new-rec.set "ename", ename}
    {new-rec.set "job", job}
    {new-rec.set "mgr", mgr}
    {new-rec.set "hiredate", hiredate}
    {new-rec.set "sal", sal}
    {new-rec.set "deptno", deptno}
    {employees.append new-rec}
}

{for emp: JsonObject in empJsonValue do
    let found?: bool = false
    let empno: int = 0
    let ename: String = {String}
    let job: String = {String}
    let mgr: int = 0
    let hiredate: double = 0
    let sal: double = 0
    let deptno: int = 0
    set (empno, found?) = {emp.get-if-exists "empno"}
    set (ename, found?) = {emp.get-if-exists "ename"}
    set (job, found?) = {emp.get-if-exists "job"}
    set (mgr, found?) = {emp.get-if-exists "mgr"}
    set (hiredate, found?) = {emp.get-if-exists "hiredate"}
    set (sal, found?) = {emp.get-if-exists "sal"}
    set (deptno, found?) = {emp.get-if-exists "deptno"}
    {if found? then
        {add-rec empno, ename, job, mgr, hiredate, sal, deptno}
    }
}

{value
    {RecordGrid
        width=700
        , record-source = employees
    }
}

まとめ

 Curlはバージョン6.0から、JSONをサポートするようになりました。その紹介も兼ねて、今回はJavaで作成したサーバーサイドとJSON形式で通信を行うアプリケーションの作成を行いました。

 次回はJSON形式ではなく、「Curl ORB for Java」というOSSのライブラリを使用して、CurlアプリケーションからPOJO(Plain Old Java Object)のメソッドをコールし、戻り値をCurlアプリケーションで受け取るアプリケーションを作成する手順を紹介する予定です。

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
Java開発者のためのCurl入門連載記事一覧

もっと読む

この記事の著者

木村聡(キムラサトシ)

2000年からJavaによるWeb開発に携わる。Seasarプロジェクトコミッタであり、コミッタとしての経験をもとに現在は仕事としてフレームワークの開発を行っている。著書に、「Javaフレームワーク開発入門」や「Eclipseで学ぶはじめてのJava」等がある。

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

【AD】本記事の内容は記事掲載開始時点のものです 企画・制作 株式会社翔泳社

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/3206 2008/10/28 14:00

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング