0

I wrote an android application that uses the component IdHTTP. The problem arises when the eighteenth procedure call.

var
  Form2: TForm2;
  XmlS: TStringlist;
  MemoryS: TMemoryStream;

  Host: string;
  IdHTTP: TIdHTTP;
  i: integer;
  response: IXMLResponseType;
procedure ReadXML;

begin
  inc(i);
  XmlS.Clear;
  MemoryS.Clear;
  Form2.XMLDocument1.active := true;

  IdHTTP.Request.Clear;
  IdHTTP.HandleRedirects := true;
  IdHTTP.Request.Username := 'adminqq';
  IdHTTP.Request.Password := 'adminw';
  IdHTTP.Request.BasicAuthentication := true;
  Host := 'http://' + form1.EHost.text + '/da1.xml';

  try
    IdHTTP.Get(Host, MemoryS)
  except
    Form2.Label1.text := 'Http Error'
  end;
  MemoryS.Position := 0;
  TEncoding.UTF8.ToString;
  XmlS.LoadFromStream(MemoryS, TEncoding.UTF8);
  Form2.XMLDocument1.LoadFromXML(XmlS.text);
  response := Getresponse(Form2.XMLDocument1);
  Form2.Label1.text := 'Temperature: ' + floattostr(response.Ia7 / 10) + '°' +
    ' Count: ' + inttostr(i);
  Form2.XMLDocument1.active := false;
  Form2.RoundRect1.Fill.Color := SwitchStatus(response.Out0);
  Form2.RoundRect2.Fill.Color := SwitchStatus(response.Out1);
  Form2.RoundRect3.Fill.Color := SwitchStatus(response.Out2);
  Form2.RoundRect4.Fill.Color := SwitchStatus(response.Out3);
  Form2.RoundRect5.Fill.Color := SwitchStatus(response.Out4);
end;

Each eighteenth procedure call closes the application, no error messages. I tried to use only:

IdHTTP.Request.Clear;
      IdHTTP.HandleRedirects := true;
      IdHTTP.Request.Username := 'adminqq';
      IdHTTP.Request.Password := 'adminw';
      IdHTTP.Request.BasicAuthentication := true;
      Host := 'http://' + form1.EHost.text + '/da1.xml';
      try
        IdHTTP.Get(Host);
      except
        Form2.Label1.text := 'Http Error'
      end;

without success (other hosts, http documents). What is wrong?

Jaszczomp
  • 1
  • 1
  • There is no way to diagnose that problem with this information. My guess is that this code is running in the main thread and is raising an uncaught exception. Add more error handling, move it to a worker thread (with proper syncing for UI updates), etc. – Remy Lebeau Jan 12 '14 at 18:14
  • Also, calling `TEncoding.UTF8.ToString` is useless, and you can get rid of `XmlS` completely and just pass `MemoryS` directly to `XMLDocument1` via its `LoadFromStream()` method. If you want to convert the downloaded XML to a String for loading (not recommended), you can let `TIdHTTP.Get()` do that for you and get rid of `MemoryS`. – Remy Lebeau Jan 12 '14 at 18:17
  • What does `Android logcat` show? What was the reason of app termination? What exactly do you mean by "without success"? – Arioch 'The Jan 12 '14 at 22:58
  • Where are xmls and memorys variables created and destroyed? You code does not show. Is XMLdocument1 dropped onto form or manually created? – Arioch 'The Jan 12 '14 at 23:00

0 Answers0