将对象传递给DataSnap Server方法?

时间:2016-05-31 13:26:30

标签: json delphi datasnap

我正在使用Delphi XE6,& datasnap,我试图将带有一些字段的Object(TDataObj)传递给datasnap服务器方法,并且发生了最奇怪的错误。如果我的数据对象传入NOT nil。

,我的服务器方法将在TStringStream创建时崩溃

//客户端

%h3
  = t("admin.labels.employee")
  = content_tag(:span, employee_counter)
- def create_validation_rules(field_rules)
- Hash[field_rules.map do |rule|
- next ["rule-#{rule[:name]}", rule[:value]] if rule[:value]
- next ["msg-#{rule[:name]}", rule[:msg]] if rule[:msg]
- end] if field_rules
- end
.control-group.form-controls
  = f.label :first_name, t("labels.name")
  = f.text_field :first_name, placeholder: t("labels.first_name"), class: 'js-employeeName', id: "merchant_employees_attributes_#{index}_first_name", name: "merchant[employees_attributes][#{index}][first_name]", autocomplete: "off", maxlength: 50, size: nil, data: create_validation_rules(validations[:first_name])
  = f.text_field :middle_name, placeholder: t("labels.middle_name"), id: "merchant_employees_attributes_#{index}_middle_name", name: "merchant[employees_attributes][#{index}][middle_name]", autocomplete: "off", maxlength: 100, size: nil
  = f.text_field :last_name, placeholder: t("labels.last_name"), class: 'js-employeeName', id: "merchant_employees_attributes_#{index}_last_name", name: "merchant[employees_attributes][#{index}][last_name]", autocomplete: "off", maxlength: 50, size: nil, data: create_validation_rules(validations[:last_name])
.control-group
  = f.label :email, t("labels.email_address")
  = f.text_field :email, id: "merchant_employees_attributes_#{index}_email", name: "merchant[employees_attributes][#{index}][email]", autocomplete: "off", maxlength: 250, size: nil, data: create_validation_rules(validations[:email])
.control-group
  - if force_superuser_role
    = f.hidden_field :superuser, value: "1"
  - else
    = f.label :superuser, t("admin.labels.superuser")
    = f.check_box :superuser, class: "superuser_checkbox", id: "merchant_employees_attributes_#{index}_superuser", name: "merchant[employees_attributes][#{index}][superuser]"

//服务器端方法

procedure TForm8.btnGetDataClick(Sender: TObject);
var
  DataObj: TDataObj;
  S: TStream;
begin
  //create an object
  DataObj := TDataObj.Create;
  DataObj.Id:= 1;
  DataObj.Name := 'DataObj1';
  DataObj.ExportType := 'PDF';
  //pass the obj to server method to retrieve data stream back
  try
    S := ClientModule1.ServerMethods1Client.GetData(DataObj);
    S.Seek(0, soFromBeginning);
    ClientDataSet1.LoadFromStream(S);
    ClientDataSet1.Open;
  finally
  end;
end;

//得到Json

function TServerMethods1.getData(const DataObj: TDataObj): TStream;
var
  r: String;
  SS: TStringStream;
begin
  //create a stream using JSON from getSessionJSON method
  SS := TStringStream.Create(getSessionJSON(DataObj)); //returns json
  try
    try
      r := ServerContainer1.idHttp1.Post
        ('MyUrlHere', SS);
      //do something else here
    except
      on e: exception do
      begin
        ShowMessage(e.Message);
        exit;
      end;
    end;
  finally
    SS.Free;
  end; 
end;

但是,在客户端,如果我传入一个nil对象

function TServerMethods1.getSessionJSON(const DataObj: TDataObj): String;
var
  ParamsJSONObj, SessionJSONObj: TJSONObject;
begin
  ParamsJSONObj := TJSONObject.Create;
  ParamsJSONObj.AddPair(TJSONPair.Create('ID', DataObj.Id));
  ParamsJSONObj.AddPair(TJSONPair.Create('Name', DataObj.Name));
  ParamsJSONObj.AddPair(TJSONPair.Create('ExportType', DataObj.ExportType));
  SessionJSONObj := TJSONObject.Create;
  SessionJSONObj.AddPair(TJSONPair.Create('DataParams', ParamsJSONObj));
  result := SessionJSONObj.toString;
end;

然后填写服务器端get Json中的值

 S := ClientModule1.ServerMethods1Client.GetData(nil);

它有效,但是,如果我传入实际数据对象(dataObj)而不是nil

它在服务器端崩溃

 ParamsJSONObj.AddPair(TJSONPair.Create('ID', '1'));
  ParamsJSONObj.AddPair(TJSONPair.Create('Name', 'DataObj1'));
  ParamsJSONObj.AddPair(TJSONPair.Create('ExportType', 'pdf'));

我是否调用了getSessionJSON

如果我放入任何字符串

  SS := TStringStream.Create(getSessionJSON(DataObj)); 

如果DataObj不是nil,则dataObj中的值存在,但是

SS:= TStringStream.Create 将引发运行时错误

希望这是可以理解的? dataObj,如果dataObj不是nil

,则使上面的调用崩溃

非常感谢任何帮助

1 个答案:

答案 0 :(得分:2)

原来是一个使用

解决的编码问题
 S := getSessionJSON(DataObj);
  SS := TStringStream.Create(r, TEncoding.ASCII);