私は Flex を初めて使用し、Web サービスのクライアントを作成するのも初めてです。私の質問は、Flex (Flash Builder 4.5) API と、どの API を使用するかについてです。
Web サービスにアクセスし、誰でも使用できる Flex / AIRwrapper を作成したいと考えています。
- POST https://build.phonegap.com/api/v1/appsに投稿する必要があります
- コンテンツ タイプは「multipart/form-data」である必要があります
リクエストの JSON ボディは「データ」という名前であることが期待され、次のようになります。
data={"title":"API V1 App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}
パラメータ名「file」を使用して、投稿のマルチパート本文に zip ファイルを含めます。
「multipart/form-data」投稿を作成し、1 つの文字列と 1 つの zip ファイルを送信したいと考えています。
私の最初の質問は、本文で文字列とバイナリ データの両方を送信した場合、サーバーは文字列の終了位置と zip ファイルの開始位置をどのように理解するかということでした。
次に、「multipart/form-data」ポスト リクエストを介してテキスト + バイナリ データを送信する方法を読みました。いくつかの境界が必要です。
この後、フレックスで例を読んで、それに従ってみました。 http://codeio.wordpress.com/2010/04/03/5-minutes-on-adobe-flex-mimic-file-upload-for-in-memory-contents/ しかし、それは機能していないようです自分。
public function createNewApp(cb:Function , appFile : File):void
{
var service:HTTPService = new HTTPService();
service.url = ROOT+"apps";
service.showBusyCursor = true;
service.addEventListener(ResultEvent.RESULT, function(e:ResultEvent):void {
//translate JSON
trace(e.result);
var result:String = e.result.toString();
var data:Object = JSON.parse(result);
cb(data.link);
});
service.addEventListener(FaultEvent.FAULT, defaultFaultHandler); //todo : allow user to add his own as well
authAndUploadNewApp(service,appFile);
}
private function authAndUploadNewApp(service:HTTPService,appFile : File):void {
var encoder:Base64Encoder = new Base64Encoder();
encoder.encode(username + ":"+password);
service.headers = {Accept:"application/json", Authorization:"Basic " + encoder.toString()};
service.method ="POST";
var boundary:String = UIDUtil.createUID();
service.contentType = "multipart/form-data; boundary=—————————" + boundary;
var stream:FileStream = new FileStream();
stream.open(appFile, FileMode.READ);
var binaryData:ByteArray = new ByteArray();
var fileData : String = new String();
stream.readBytes(binaryData);
stream.close();
fileData = binaryData.readUTFBytes(binaryData.bytesAvailable); // I think this is where I have problem.... how do
//how do i converrt this bytearray/stream of data to string and send it in my post request's body - i guess if this step work rest should work..
var params: String = new String();
var content:String = "—————————" + boundary + "nr";
content += 'Content-Disposition: form-data; name="data";' + '{"title":"ELS test app 2","package":"com.elsapp.captivate","version":"12.3.09","create_method":"file"}' + "nr";
content += "—————————" + boundary + "nr";
content += 'Content-Disposition: form-data; name="file";' + fileData + "nr";
content += "—————————–" + boundary + "–nr";
service.request = content;
service.send();
}