Jump to content

TW3HttpRequest response content to TStringList items?


Recommended Posts

I am making a HTTP request with TW3HttpRequest and I receive the correct data.

 

So far so good.

 

However, the data that I am sending from my server to my SMS project is sever lines of strings.

 

Using ShowMessage() to show the response data does indeed show a message with multiple lines of strings.

 

Great!

 

However, when I load the response in to a TStringList and try to read a specific item (line of string),

it turns out that there is only one item, and that item contains all of the lines of strings that my server responds with.

 

What is going on here?

Link to post
Share on other sites
You could create a custom service and send a JSON array object

 



var
mList: TStringlist;
Res : variant;
begin
mlist:=TStringList.Create;
res := JSON.Parse('[{"ID":1,"FirstName":"John"},{"ID":2,"FirstName":"Arnaud"},{"ID":3,"FirstName":"Don"}]');

for var x:= 0 to Length(res)-1 do
begin
mList.Insert(x, res[x].FirstName);
end;

WriteLn(mList.Items[0]); // John
WriteLn(mList.Items[1]); // Arnaud
WriteLn(mList.Items[2]); // Don

mList.Free;

Link to post
Share on other sites

Can you post code on how you are assigning the multi-line text to the stringlist?

 

I'm basically doing the following

myStringList.Text := Sender.ResponseText;

 

You could create a custom service and send a JSON array object
 
var
  mList:  TStringlist;
  Res : variant;
begin
  mlist:=TStringList.Create;
  res := JSON.Parse('[{"ID":1,"FirstName":"John"},{"ID":2,"FirstName":"Arnaud"},{"ID":3,"FirstName":"Don"}]');

  for var x:= 0 to Length(res)-1 do
  begin
    mList.Insert(x, res[x].FirstName);
  end;

  WriteLn(mList.Items[0]);  // John
  WriteLn(mList.Items[1]);  // Arnaud
  WriteLn(mList.Items[2]);  // Don

  mList.Free;

 

 

I believe that it would take me an unnecessary amount of time to do all of that work for a 10+ lines text.

 

I'll keep that in mind though, all of a sudden the requirements of my project might change.

 

Thank you very much for sharing the code!

Link to post
Share on other sites

> Ok I've changed my server code so it responds with an XML file.

 

> Where can I find information about how to parse the XML file in my SMS project?

 

XML is organized as DOM so you'll have similar structure as in html page and you can access it with JXMLDocument and then access nodes with getElementsByTagName for example. 

BTW I would suggest to use JSON instead, there is no parsing at all, you'll get values in some variant variable and you can access them directly. There was a sample of it in one of earlier messages in this thread.

Link to post
Share on other sites

> Ok I've changed my server code so it responds with an XML file.

 

> Where can I find information about how to parse the XML file in my SMS project?

 

XML is organized as DOM so you'll have similar structure as in html page and you can access it with JXMLDocument and then access nodes with getElementsByTagName for example. 

BTW I would suggest to use JSON instead, there is no parsing at all, you'll get values in some variant variable and you can access them directly. There was a sample of it in one of earlier messages in this thread.

 

Ok, thank you.

 

I want to try the XML route first, but I can't find any examples about XML and XML parsing.

 

Does anyone have any code snippets I can use?

 

I basically need to:

1. Create an XML parsing object

2. Load the data as string

3. Iterate through each element

4. Iterate through each sub-elemet

 

I solved it.

 

Everything is available in Sender.Content

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...