Home > Back-end >  Ask: simple Delphi submitted to the web server problems, beginners, give advice or comments please!
Ask: simple Delphi submitted to the web server problems, beginners, give advice or comments please!

Time:09-16

Development environment: WIN10 family, Delphi XE4
Functional requirements: Delphi use IdHTTP controls to a web server sends a request to receive the data returned, the server support for XML and JSON returns, notes mentioned need -- "the client specified ContentType=" application/JSON ", the server returns a JSON format "
Request statement: http://* * * *. * *. * *, * */* */API/getlastinfo? Tid=2123123 & amp; Key=1-2342-3546-2232-2342

I use the Edge directly input the above link in your browser, to return to normal:
{" dar ":" 2123123 ", "Time" : 0, "Dir" : 240, "Mileage" : 0 "Addr" : {" Province ", "shandong Province", "City" : "jinan"},}

I use the DELPHI writing, the code is as follows:
Url:='http://* * * *. * *. * *, * */API/* */getlastinfo? Tid=2123123 & amp; Key=1-2342-3546-2232-2342 ';
IdHTTP. Get (url, ts);//ts for TStringStream
Ts. Position:=0;
Ajson:=SuperObject. So (ts) DataString);//ajson ISuperObject
Dir:=ajson [' dir '] AsInteger;//this sentence error
I dropped my error statements comments, plus the Memo1. Lines. The Add (ajson. AsString) are identical; Want to see the return is correct, the result Memo1 display content is as follows:
0 240 & lt;/Dir> The xian 庡 崡 ning?/City> Ba variable Min e ?/Province>
Return should not JSON type, and still gibberish, clearly the browser test pass, why not, if use IdHTTP request error?
Online, etc., thank you!

CodePudding user response:

Great god, please feel free to comment

CodePudding user response:

Not said the JSON format specified in the requirements? You add to go in and see the results,
IdHTTP1. Request. ContentType:='application/json;//set the content type for json

CodePudding user response:

Results so try: (it's best to showmessage (ts) DataString)) to see if the results right
Jo: SO=(UTF8Decode (HttpDecode (ts) DataString)));
Dir: Jo=[' dir '] Asinteger;

CodePudding user response:

refer to the second floor shadowpj response:
not said the JSON format specified in the requirements? You add to go in and see the results,
IdHTTP1. Request. ContentType:='application/json;//set the content type as json


I have this set, return the data or: & lt; TDetail XMLNS: I="http://www.w3.org/2001/XMLSchema-instance" XMLNS="http://schemas.datacontract.org/2004/07/CloudMap.Models" & gt; 240 & lt;/Dir> The xian 庡 崡 ning?/City> Ba variable Min e ?/Province>

CodePudding user response:

reference shadowpj reply: 3/f
results so try: (it's best to showmessage (ts) DataString)) to see if the results right
Jo: SO=(UTF8Decode (HttpDecode (ts) DataString)));
Dir: Jo=[' dir '] Asinteger;


Thank you, this should be able to eliminate noise, but still can't make the JSON data from server

CodePudding user response:

Initialization Settings
Idhtp1. HandleRedirects:=true;
Idhtp1. Request. ContentType:='application/x - WWW - form - urlencoded';
Idhtp1. Request. ContentEncoding:="utf-8";
Idhtp1. ProtocolVersion:=pv1_1;

CodePudding user response:

Interface with postman first test, no problem again handler, and you receive data did not do any inspection, no exception handling,