1
我需要调用ASP web api并从delphi 2007返回JSON。我可以在TR StudioClient的RAD Studio XE 5中执行此操作。我试图把它放在一个dll中,以便我可以从我的delphi 2007程序中调用它。但没有成功。我如何使用delphi 2007来做到这一点?如何调用ASP web api并从delphi 2007返回JSON
编辑
这里就是我想在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(零);”当放入一个DLL。
你究竟做了什么?什么不适合你?如果你需要帮助,你需要提供具体的细节。 –
Indy应该工作得很好 - 您只需要使用Superobjects Lib或类似工具解析JSON即可。在表单上放置TIDHTTP组件或将其实例化 - > str:= TIDHTTP.get(** resturl ***) –
@Remy对不起,对于迟到的回复。我用示例代码更新了我的问题。 – Reynaldi