Authentication when WS is called in Flash _ 5: passing the array to ws

Source: Internet
Author: User
We have done an example of getting data from WS to fill in the Flash DataGrid. In practice, we often need to pass the modified data in the DataGrid back to the server again to make the data persistent on the server.
In flash, there are many ways to transmit data back to the server for storage: LoadVars, splicing xml, etc. The operation process is cumbersome and not easy to use. There are still many restrictions. If you use Flash + WS, that sentence is very convenient. Let's talk about it with examples.
1. Modify sample2.asmx. cs and add the Web method GetArray_4. The web method has an array of tCar parameters.
[WebMethod (Description = "accept excessive array transmitted by client")]
Public string GetArray_4 (Car [] tCar ){
String tStr = "";
For (int I = 0; I <tCar. Length; I ++ ){
TStr = tStr + ";" + tCar [I]. model;
}
Return tStr;
}
2. Modify Flash as follows:
Function GetArray_4 (result ){
Trace ("the value returned by the ws method GetArray_4 is :");
Trace ("/////////////////////////");
Trace (result );
}
......
This. dbBind3_bt.onPress = function (){
// Trace (gb_main.dataProvider.length );
Var I: Number = 0;
Var tCar: Array = new Array ();
TCar = gb_main.dataProvider;
Var op_8: PendingCall = myws. GetArray_4 (tCar );
Op_8.onResult = GetArray_4;
}

Run, the output window will prompt:
Error opening URL http: // localhost/ws/sample2.asmx

Why? The reason lies in the dataProvider of the DataGrid component. You can trace the array provided by dataProvider and find that dataProvider does not know why and adds some objects with unknown data types. But there is no doubt, the Car [] object array can be passed to ws through Flash.
The modified code is as follows:
/* ===================================================== ======================================

C # Source File -- Created with SAPIEN technology Primalcode 3.0

NAME: sample2.asmx. cs

AUTHOR: JimLee, Dxl School
DATE: 2004-10-7

COMMENT: This example demonstrates the simple type, date, and time of accepting numbers from the client in ws.
Type, bool type, name, string simple type, and Array type parameters and return values.

========================================================== ===================================== */

Using System;
Using System. Collections;
Using System. Web;
Using System. Web. Services;

Namespace wsLearn {
[WebService (Namespace = "http://www.dxlschool.com/ws/", Description = "Example 2, demonstrate the passing value of the ws method", Name = "s2")]
Public class sample2: System. Web. Services. WebService {
Public sample2 (){
//
}

[WebMethod]
Public string GetUserName (string tName ){
Return "hello," + tName;
}

[WebMethod]
Public int IncAB (int A, int B ){
Return A + B;
}

[WebMethod]
Public DateTime GetNow (){
Return DateTime. Now;
}

[WebMethod]
Public DateTime PassDate (DateTime dt ){
Return dt;
}

[WebMethod]
Public bool PassBool (bool bl ){
Return bl;
}

[WebMethod]
Public string [] GetArray_1 (){
String [] strArr = {"Joe dog", "Liu Peng", "Chen Bo", "Zhang chengping", "Fei Ming", "Chen Gang "};
Return strArr;
}

[WebMethod (Description = "array call: ArrayList")]
Public ArrayList GetArray_2 (){
ArrayList strArr = new ArrayList ();
Int I = 0;
For (I = 0; I <100; I ++ ){
StrArr. Add ("strArr [" + I + "]");
}
Return strArr;
}

[WebMethod (Description = "array call, including object")]
Public Car [] GetArray_3 (){
Car HG = new Car ("crown", 2004 );
Car JM = new Car ("beautiful", 2003 );
Car YK = new Car ("accord", 2004 );
Car BJS = new Car ("Picasso", 2002 );
/* ArrayList carArr = new ArrayList ();
CarArr. Add (HG );
CarArr. Add (JM );
CarArr. Add (YK );
CarArr. Add (BJS );*/

Car [] carArr = {HG, JM, YK, BJS };
Return carArr;
}

[WebMethod (Description = "accept excessive array transmitted by client")]
Public string GetArray_4 (Car [] tCar ){
String tStr = "";
For (int I = 0; I <tCar. Length; I ++ ){
TStr = tStr + ";" + tCar [I]. model;
}
Return tStr;
}
}

[Serializable]
Public class Car {
// Declare the field model and yearBuilt;
Public string model;
Public int yearBuilt;

// Define the constructor
Public Car (string model, int yearBuilt ){
This. model = model;
This. yearBuilt = yearBuilt;
}

Public Car (){

}
}

}
/**
ActionScript Class File -- Created with SAPIEN Technologies PrimalScript 3.0

@ Class wsSample_2
@ Package wsSample_2.as
@ Author JimLee
@ Codehint
@ Example
@ Tooltip
*/

Import mx. services .*;

Gb_main.vScrollPolicy = "off ";

Var myws: WebService = new WebService ("http: // localhost/ws/sample2.asmx? Wsdl ");

Function GetUserName (result ){
Trace ("ws returned value :");
Trace ("///////////////////////////");
Trace (result );
}

Function IncAB (result ){
Trace ("the value returned by the IncAB of the ws method is :");
Trace ("///////////////////////////");
Trace (result );
}

Function GetNow (result ){
Trace ("the value returned by the ws method GetNow is :");
Trace ("/////////////////////////");
Trace (result );
}

Function PassDate (result ){
Trace ("the value returned by the ws method PassDate is :");
Trace ("/////////////////////////");
Trace (result );
}

Function PassBool (result ){
Trace ("the value returned by the ws method PassBool is :");
Trace ("/////////////////////////");
Trace (result );
}

Function GetArray_1 (result ){
Trace ("the value returned by the ws method GetArray_1 is :");
Trace ("/////////////////////////");
For (var I = 0; I <result. length; I ++ ){
Trace (I + ":" + result [I]);
}
}

Function GetArray_3 (result ){
Var myDB: Array = result;
Gb_main.dataProvider = myDB;
Trace (gb_main.dataProvider.length );
}

Function GetArray_4 (result ){
Trace ("the value returned by the ws method GetArray_4 is :");
Trace ("/////////////////////////");
Trace (result );
}

This. GetUN_bt.onPress = function (){
Var op_1: PendingCall = myws. GetUserName ("Joe !! ");
Op_1.onResult = GetUserName;
}

This. incAB_bt.onPress = function (){
Var a: Number = 34;
Var B: Number = 56;
Var op_2: PendingCall = myws. IncAB (a, B );
Op_2.onResult = IncAB;
}

This. GetNow_bt.onPress = function (){
Var op_3: PendingCall = myws. GetNow ();
Op_3.onResult = GetNow;
}

This. PassDT_bt.onPress = function (){
Var dt: Date = new Date (77, 4, 2 );
Var op_4: PendingCall = myws. PassDate (dt );
Op_4.onResult = PassDate;
}

This. PassBool_bt.onPress = function (){
Var bl: Boolean = true;
Var op_5: PendingCall = myws. PassBool (bl );
Op_5.onResult = PassBool;
}

This. dbBind1_bt.onPress = function (){
Var op_6: PendingCall = myws. GetArray_1 ();
// Var op_6: PendingCall = myws. GetArray_2 ();
Op_6.onResult = GetArray_1;
}

This. dbBind2_bt.onPress = function (){
Var op_7: PendingCall = myws. GetArray_3 ();
Op_7.onResult = GetArray_3;
}

This. dbBind3_bt.onPress = function (){
// Trace (gb_main.dataProvider [1]);
Trace (gb_main.dataProvider.length );
Var I: Number = 0;
Var tCar: Array = new Array ();
For (I = 0; I <4; I ++ ){
TCar. push (gb_main.dataProvider [I]);
}
// TCar = gb_main.dataProvider;
For (I = 0; I <(tCar. length/2-1); I ++ ){
Trace (tCar [I]. model );
}
Var op_8: PendingCall = myws. GetArray_4 (tCar );
Op_8.onResult = GetArray_4;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.