Corona SDKでHTTP通信
Corona SDKでHTTP通信
- newtwork.request
networkライブラリのrequestメソッドを使用します
Corona Docs — API | Libraries | network | request
http,httpsの非同期通信リクエストを作成します
-
request( url, method, listener [, params] )
- 戻り値
- requestId
network.cancel(requestId)とすることでその通信をキャンセルします
- requestId
- 引数
- url
通信するURL ・String - method
GETやPOSTやPUTなどメソッド名を指定 ・String - listener
通信中(progress)、通信終了後のコールバック ・function- callback引数
- event
Corona Docs — API | Events | networkRequest- .isError,.name,.phase,.requestId,.status,.url,.responseHeaders,.responseType,.response,.bytesTransferred,.bytesEstimated
- event
- callback引数
- param
パラメータの指定 ・table- headers
- ヘッダ ・table
- body
・String - その他のパラメータ
bodytype,progress,response,timeout,handleRedirects
- headers
- url
- 戻り値
というのが基本です
- 使用例
local function network_listener(event) --コールバックの関数 if event.isError then print('network error') else print ( "RESPONSE: " .. event.response ) end end lcocal url = 'https://www.google.com' local method = 'GET' local params = { headers = { ['Accept-Language'] = 'ja-JP' } timeout = 15 } network.request(url,method,network_listener,params)
- POST
JsonでパラメータをPOSTしたい場合
local function network_listener(event) --コールバックの関数 if event.isError then print('network error') else print ( "RESPONSE: " .. event.response ) end end local url = 'https://www.google.com' local method = 'POST' local params = { headers = { ['Content-Type'] = 'applicatdon/x-www-form-urlencoded' }, body = 'color=red&size=small' } network.request(url,method,network_listener,params)
- headersにContent-Type = 'applicatdon/x-www-form-urlencoded'を指定して、bodyのパラメータは文字列型でkey=valueで&つなぎで指定する必要があります