【问题标题】:How to call an ASP web api and return JSON from delphi 2007如何调用 ASP Web api 并从 delphi 2007 返回 JSON
【发布时间】:2015-01-05 11:13:06
【问题描述】:

我需要调用一个 ASP web api 并从 delphi 2007 返回 JSON。我可以在 RAD Studio XE 5 中使用 TRestClient 完成此操作。我试图把它放在一个 dll 中,以便我可以从我的 delphi 2007 程序中调用它。但没有成功。如何使用 delphi 2007 做到这一点?

编辑

这是我在 delphi xe 5 中尝试做的事情

class function TSampleApp.Hello(AModel: TModel): Integer;
var
  aRestClient: TRESTClient;
  aRestRequest: TRESTRequest;
  aRestResponse: TRESTResponse;
  aParam: TRESTRequestParameter;     
  jValue: TJSONValue;
  jObject: TJSONObject;
begin
  Result := -1;
  aRestClient := TRESTClient.Create(nil);
  try
    aRestResponse := TRESTResponse.Create(nil);
    try
      aRestRequest := TRESTRequest.Create(nil);
      try
        try          
          aRestClient.BaseURL := 'http://localhost:49272/api/test';
          aRestRequest.Client := aRestClient;
          aRestRequest.Response := aRestResponse;
          aRestRequest.Method := rmPOST;
          aRestRequest.Resource := 'hello';
          aParam := aRestRequest.Params.AddItem;
          aParam.Kind := pkREQUESTBODY;
          aParam.name := 'helloData';
          aParam.Value := TJson.ObjectToJsonString(AModel);

          aRestRequest.Execute;

          jValue := aRestResponse.JSONValue;
          jObject := TJSONObject.ParseJSONValue(jValue.ToString) as TJSONObject;
          Result := StrToIntDef((jObject.Get('status').JsonValue as TJSONString).Value, -1);
        finally
          FreeAndNil(jObject);
          FreeAndNil(jValue);
        end;
      finally
        FreeAndNil(aRestRequest);
      end;
    finally
      FreeAndNil(aRestResponse);
    end;
  finally
    FreeAndNil(aRestClient);
  end;
end;

此代码在 win32 应用程序中完美运行,但在“aRestResponse := TRESTResponse.Create(nil);”上失败放入 dll 时。

【问题讨论】:

  • 您到底尝试了什么?到底什么对你不起作用?如果您需要帮助,您需要提供具体的详细信息。
  • Indy 应该可以正常工作 - 您只需要使用 Superobjects Lib 或类似文件解析 JSON。将 TIDHTTP 组件拖放到表单上或实例化它 -> str := TIDHTTP.get(resturl*)
  • @Remy 抱歉回复晚了。我用示例代码更新了我的问题。
  • @Ryno 这就是我所做的,它奏效了:)
  • 对评论投赞成票会很好:)

标签: delphi delphi-xe5 delphi-2007


【解决方案1】:

我没有找到 delphi 2007 的 rest 客户端解决方案。我最终为此使用了 indy。 我使用LkJson 来处理json。

class function TSampleApp.Hello(AModel: TModel): Integer;
var
  idHttp: TIdHTTP;
  url, sjsonresponse, sjsonrequest: string;
  strRequest: TStrings;
  jsonObj: TlkJSONobject;
begin
  Result := -1;
  url := 'http://localhost:49272/api/test/hello';
  idHttp := TIdHTTP.Create;
  try
    jsonObj := TlkJSONobject.Create;
    try
      //populate
      jsonObj.Add('param1', AModel.param1);
      jsonObj.Add('param2', AModel.param2);
      sjsonrequest := TlkJSON.GenerateText(jsonObj);
    finally
      FreeAndNil(jsonObj);
    end;

    idHttp.Request.Accept := 'application/json';   
    strRequest := TStringList.Create;
    try
      strRequest.Values['helloData'] := sjsonrequest;
      sjsonresponse := idHttp.Post(url, strRequest);
    finally
      FreeAndNil(strRequest);
    end;

    jsonObj := TlkJSON.ParseText(sjsonresponse) as TlkJSONobject;
    try
      Result := StrToIntDef(VarToStr((jsonObj.Field['status'] as TlkJSONnumber).Value), -1);
    finally
      FreeAndNil(jsonObj);
    end;
  finally
    idHttp.Free;
  end;
end;

此代码也适用于 dll。

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 2019-04-22
    • 2017-01-08
    • 2014-12-15
    • 1970-01-01
    • 2020-02-05
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多