Recent

Author Topic: How to send a http request  (Read 867 times)

mpc823

  • Newbie
  • Posts: 4
How to send a http request
« on: February 06, 2023, 12:37:44 am »
Hi

I would like to send a http request to my tasmota device. I tried it first on a windows program and it worked.

The I did the same for a Android App but the call "TextView1.Text:=TFPHTTPClient.SimpleGet('http://192.168.0.28/cm?cmnd=Power%20TOGGLE');" did not work. By pressing the button the APP stops immediately. By comment the line the App dont crash so it have to be this line , which is going wrong.

Windows code working :
Quote
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  Memo1.Text:=TFPHTTPClient.SimpleGet('http://192.168.0.28/cm?cmnd=Power%20TOGGLE');
  If Memo1.Text = '{"POWER":"ON"}'
  then  SpeedButton1.Font.Color:=clLime
  else  SpeedButton1.Font.Color:=clRed;
end;

I use this as basis and it crash also , I found several demos but they did not working.


Android:
Quote

procedure TAndroidModule1.Button1Click(Sender: TObject);
Var
  URL, S : String;
  Client: TFPHttpClient;
begin
  Client := TFPHttpClient.Create(nil);
  try
    { Allow redirections }
    Client.AllowRedirect := true;
    TextView1.Text:= TFPCustomHTTPClient.SimpleGet('192.168.0.28/cm?cmnd=Power%20TOGGLE');
  finally
    Client.Free;
  end;
  If  TextView1.Text = '{"POWER":"ON"}'
  then  Button1.FontColor:=colbrLime
  else  Button1.FontColor:=colbrRed;
end;     

Learninng a lot, but it would be nice after 2 days of try and error without final sucess , if somebody could give me the right line for Lazarus for Android with LAMW, to do the same thing under Android

I know my coding style is very poor but this is my first APP ever.

here is the content of the Android unit1 maybe there is missing something I dont see and the compiler did not complain  :(

Quote
{hint: Pascal files location: ...\AppLAMWProject6\jni }
unit unit1;

{$mode delphi}

interface

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, AndroidWidget, Laz_And_Controls, fphttpclient, regexpr;
 
type

  { TAndroidModule1 }

  TAndroidModule1 = class(jForm)
    Button1: jButton;
    Button2: jButton;
    TextView1: jTextView;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    {private declarations}
  public
    {public declarations}
  end;

var
  AndroidModule1: TAndroidModule1;

implementation
 
{$R *.lfm}
 

{ TAndroidModule1 }

procedure TAndroidModule1.Button2Click(Sender: TObject);
begin
  close;
end;

procedure TAndroidModule1.Button1Click(Sender: TObject);
Var
  URL, S : String;
  Client: TFPHttpClient;
begin
  Client := TFPHttpClient.Create(nil);
  try
    { Allow redirections }
    Client.AllowRedirect := true;
    TextView1.Text:= TFPCustomHTTPClient.SimpleGet('192.168.0.28/cm?cmnd=Power%20TOGGLE');
  finally
    Client.Free;
  end;
  If  TextView1.Text = '{"POWER":"ON"}'
  then  Button1.FontColor:=colbrLime
  else  Button1.FontColor:=colbrRed;
end;

end.                     

Thanks Martin

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: How to send a http request
« Reply #1 on: February 06, 2023, 11:09:01 am »
You could search lamw http client component, it works very well.

mpc823

  • Newbie
  • Posts: 4
Re: How to send a http request
« Reply #2 on: February 11, 2023, 04:55:19 pm »
You could search lamw http client component, it works very well.

I know that you want to help me with you answer, but my knowledge about programming and Lazarus was so low that I don't understand what this should help me.

Now after find out "howto" I know and understand what you mean. I did not know that there is a component "lamw http client component" to drag and drop on the form. I thought its a buildin comannd like in the windows form I had used. I even did not know about using the components but , I know this is my ignorance sorry  %)

But know it works (not perfect) . I still try to find out some strange behavior . There is a nice webside with also the Laz2android source for an AVM device on https://devwelt.de/myria-de/lamw/-/tree/master and after reading and reading the code the picture how this have to work got better and better.


Here is my crappy code which works more or less developed by "try & error" .

Quote
procedure TAndroidModule1.Button1Click(Sender: TObject);
var
   content: string;
   response: string;
   ipadress: string;
   url: string;
begin
  ipadress := Preferences1.GetStringData('myStrData', '');
  url:= 'http://' + ipadress + '/cm?cmnd=Power%20TOGGLE';
  If ipadress = '' then begin
  ShowMessage('erst eine IP eingeben');
  end
  else begin
  try
    content := HttpClient1.Get('http://' + ipadress + '/cm?cmnd=Power%20TOGGLE');
    //ShowMessage(url);
    ShowMessage('Türe öffnen');
  finally
    //HttpClient1.Free;
  end;
  end;
  TextView1.text := content;
  response := Copy(content,11,2);
  //If response = 'ON'
  //then  Button1.FontColor:=colbrLimeGreen
  //else  Button1.FontColor:=colbrFireBrick;
end;     

for all beginners :
In Lazarus there is a tab "Android bridges extra" . Inside there you can find the "lamw http client component" mentioned from Mongkey :-) . Drag and drop now the component "HTTPClient" shown in the pic below to your form. then you can use this magic function
Quote
result : = HTTpClient1.Get(Stringurl: String) : String;

I hope this will help others a little .

regards Martin

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to send a http request
« Reply #3 on: February 13, 2023, 09:23:57 am »
I thought its a buildin comannd like in the windows form I had used.
There's no built-in "command" for HTTP calls, they're implemented as libraries. Android is a little bit alien from other Linux based OS, as it's aimed at the masses from the ground up, therefore its security is hardened from the start. TFPHTTPClient is implemented using low level socket calls, which are only accessible in Android through root (or other mechanism that gives similar access). You can ensure this by checking the logcat (this is the built-in logging system in Android) and you will very likely to be presented error 13, which is a permission denied. The jHTTPClient component, however, wraps permission granted Android Java class implementing this functionality. At the worst, you may only need to declare INTERNET permission in your application manifest and it's gonna be able to hit any HTTP endpoint in the whole internet, provided you have internet access.

 

TinyPortal © 2005-2018