LoginSignup
0
0

More than 5 years have passed since last update.

jsonを取得、表示するアプリケーション その4

Posted at

GET、POSTの両方でリクエストパラメータを指定できるように対応。
タブペインでパラメータとレスポンスを切り替えて表示。送信ボタンを押下してレスポンスが返ってきたらレスポンスタブを表示する。

json_client.groovy
import groovy.swing.SwingBuilder
import javax.swing.*
import java.awt.*

def cbMethod
def txtUrl
def txtParam
def txtJson

def tabPane

def sendRequest = {
    def url = txtUrl.text
    if (cbMethod.selectedItem == 'GET') {
        url = "${url}?${txtParam.text}"
    }

    def conn = url.toURL().openConnection()
    conn.requestMethod = cbMethod.selectedItem
    if (cbMethod.selectedItem == 'POST') {
        conn.doOutput = true
        conn.outputStream << txtParam.text
    }

    txtJson.text = conn.inputStream.text
    tabPane.selectedIndex = 1
}

new SwingBuilder().frame(title:'jsonを取得、表示するアプリケーション', defaultCloseOperation:JFrame.EXIT_ON_CLOSE,
                        size: [800, 600], show:true) {
    lookAndFeel('nimbus')
    borderLayout()
    hbox(constraints: BorderLayout.NORTH) {
        cbMethod = comboBox(items:['GET', 'POST'])
        txtUrl = textField()
        button(text:'送信', actionPerformed:sendRequest)
    }
    tabPane = tabbedPane(constraints: BorderLayout.CENTER) {
        scrollPane(name:'パラメータ') {
            txtParam = textArea()
        }
        scrollPane(name:'レスポンス') {
            txtJson = textArea(editable:false)
        }
    }
}

qiita-004a.png
qiita-004b.png

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