2009年1月7日水曜日

JavaFX HttpRequest (io.http)

JavaFX で http リクエスト

GET request

  1. var s : String = "";  
  2.   
  3. def request : HttpRequest = HttpRequest {  
  4.     location: "http://javafx.com";  
  5.   
  6.     onStarted: function() {  
  7.         s = "started"  
  8.     }  
  9.     onConnecting: function() {  
  10.         s = "{s}\nconnecting..."  
  11.     }  
  12.     onDoneConnect: function() {  
  13.         s = "{s}\ndoneConnect"  
  14.     }  
  15.     onReadingHeaders: function() {  
  16.         s = "{s}\nreadingHeaders..."  
  17.     }  
  18.     onResponseCode: function(code:Integer) {  
  19.         s = "{s}\nresponseCode: {code}"  
  20.     }  
  21.     onResponseMessage: function(msg:String) {  
  22.         s = "{s}\nresponseMessage: {msg}"  
  23.     }  
  24.   
  25.     onResponseHeaders: function(headerNames: String[]) {  
  26.         s = "{s}\nthere are {headerNames.size()} response headers:";  
  27.         for (name in headerNames) {  
  28.             s = "{s}\n    {name}: {request.getResponseHeaderValue(name)}"  
  29.         }  
  30.     }  
  31.   
  32.     onToRead: function(bytes: Integer) {  
  33.         s = "{s}\nbytes to read: {bytes}"  
  34.     }  
  35.   
  36.     onRead: function(bytes: Integer) {  
  37.         def progress =  
  38.         if (request.toread > 0"({(bytes * 100 / request.toread)}%)" else "";  
  39.         s = "{s}\nbytes read: {bytes} {progress}";  
  40.     }  
  41.   
  42.     onInput: function(is: java.io.InputStream) {  
  43.         try {  
  44.             s = "{s}\nbytes of content available: {is.available()}"  
  45.         } finally {  
  46.             is.close();  
  47.         }  
  48.     }  
  49.   
  50.     onException: function(ex: Exception) {  
  51.         s = "exception: {ex.toString()}"  
  52.     }  
  53.     onDoneRead: function() {  
  54.         s = "{s}\ndoneRead"  
  55.     }  
  56.     onDone: function() {  
  57.         s = "{s}\ndone"  
  58.     }  
  59. }  
  60.   
  61. Stage {  
  62.     title: "MyApp"  
  63.     scene: Scene {  
  64.         width: 400  
  65.         height: 600  
  66.         content:Text {  
  67.             font: Font {  
  68.                 size: 11  
  69.             }  
  70.             x: 10,  
  71.             y: 30  
  72.             content: bind s  
  73.         }  
  74.     }  
  75. }  
  76.   
  77. request.enqueue();  


実行結果



variables
input : InputStream
location : "http://javafx.com"
method : String ("GET/POST/PUT/DELETE")
output : OutputStream (only POST or PUT)
POST : String (only method = POST)
PUT : String (only method = PUT)
read : Integer (read bytes)
responseCode : Integer
responseMessage : String
toread : Integer (be expected read bytes)
towrite : Integer (going to be written bytes)
written : Integer (have been written bytes)


triggers
onStarted: function() {}
onConnecting: function() {}
onDoneConnect: function() {}
onReadingHeaders: function() {}
onResponseCode: function(code:Integer) {}
onResponseMessage: function(msg:String) {}
onResponseHeaders: function(headerNames: String[]) {}
onToRead: function(bytes: Integer) {}
onRead: function(bytes: Integer) {}

// Be sure to close the input sream when finished
onInput: function(input: java.io.InputStream) {
try {

} finally {
is.close();
}
}

onException: function(ex: Exception) {}
onDoneRead: function() {}
onDone: function() {}

0 件のコメント:

コメントを投稿