【发布时间】: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