procedure TForm1.Button1Click(Sender: TObject);
var ReadError : Boolean;
url : UTF8String;
n,i : integer;
Writer: TJsonTextWriter;
Reader: TJsonTextReader;
StringWriter: TStringWriter;
DataIn : TMemoryStream;
DataOut : TMemoryStream;
Buf : AnsiString;
ResultString : TStringList;
Root: TRoot;
S :
String;
begin
StringWriter := TStringWriter.Create();
Writer := TJsonTextWriter.Create(StringWriter);
Writer.Formatting := TJsonFormatting.Indented;
Writer.WriteStartObject;
Writer.WritePropertyName('
address');
Writer.WriteStartObject;
Writer.WritePropertyName('
regionCode');
Writer.WriteValue('
DE');
Writer.WritePropertyName('
addressLines');
Writer.WriteStartArray;
S := '
Seestraße';
Writer.WriteValue(S);
Writer.WriteValue('
77694 Kehl');
Writer.WriteEndArray;
Writer.WriteEndObject;
Writer.WriteEndObject;
Memo1.Text := StringWriter.ToString;
Buf := StringWriter.ToString;
ReadError := False;
url := '
https://addressvalidation.googleapis.com/v1:validateAddress?key=XXXX';
DataIn := TMemoryStream.Create;
{ For the response }
DataOut := TMemoryStream.Create;
{ For the data to be sent }
DataOut.
Write(Buf[1], Length(Buf));
DataOut.Seek(0, soFromBeginning);
try
SslHttp.URL :=
Url;
SslHttp.ContentTypePost := '
application/json';
SslHttp.SendStream := DataOut;
SslHttp.RcvdStream := DataIn;
SslHttp.Post;
except
on e:
Exception do
begin
ReadError := True;
ShowMessageFmt ('
Error: %s', [e.
Message]);
end;
end;
if not ReadError
then begin
DataIn.Seek(0, 0);
ResultString := TStringList.Create;
ResultString.LoadFromStream(DataIn);
Memo1.Lines.Add(ResultString.Text);
Root := TRoot.Create;
Root.AsJson := ResultString.Text;
Memo1.Lines.Add(Root.Result.Address.PostalAddress.PostalCode);
Memo1.Lines.Add(Root.Result.Address.FormattedAddress);
ResultString.Free;
end;
DataIn.Free;
DataOut.Free;
end;