Recent

Author Topic: Problem with this code.....can you help me??  (Read 793 times)

lestroso

  • Full Member
  • ***
  • Posts: 107
    • FaSasoftware
Problem with this code.....can you help me??
« on: May 25, 2020, 08:09:59 pm »
Hi, i'm trying to work with this software...but ..if i try this software ....lazarus give me an error and application block itself..
The software check if is there an internet connection ready, if yes take data from s .....(google.com)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.      begin
  3.   with TFPHttpClient.Create(nil) do begin
  4.       s:= get('https://www.google.com');
  5.      if  s <> ('')
  6.     then ShowMessage (copy(s,1,50))
  7.     else ShowMessage ('Please Connect To Internet!!');
  8. free;
  9.  end;
  10. end;
  11.  
  12. end.      
  13.  
Thank you a lot In advance,Lestroso :-[

bytebites

  • Sr. Member
  • ****
  • Posts: 281
Re: Problem with this code.....can you help me??
« Reply #1 on: May 25, 2020, 08:46:29 pm »
What error Lazarus gives?

lestroso

  • Full Member
  • ***
  • Posts: 107
    • FaSasoftware
Re: Problem with this code.....can you help me??
« Reply #2 on: May 25, 2020, 09:03:34 pm »
here the error message....i enclose the screenshot....thanks Lestroso :-[


kqha

  • New Member
  • *
  • Posts: 12
Re: Problem with this code.....can you help me??
« Reply #3 on: May 26, 2020, 02:15:57 pm »
You should enclose the get(...) inside try...except block, from the error message fphttpclient raise an exception when it cannot connect and/or resolve the url.

Something like this should do the work:
Code: Pascal  [Select][+][-]
  1. with TFPHttpClient.Create(nil) do begin
  2.   try
  3.     s:= get('https://www.google.com');
  4.     if  s <> ('') then ShowMessage (copy(s,1,50))
  5.     else ShowMessage ('Please Connect To Internet!!');
  6.   except
  7.     on e:Exception do ShowMessage ('Error connecting, message: '+e.Message);
  8.   end;
  9. end;
  10.  

lestroso

  • Full Member
  • ***
  • Posts: 107
    • FaSasoftware
Re: Problem with this code.....can you help me??
« Reply #4 on: May 26, 2020, 04:13:38 pm »
Dear kqha,
i tryed your code but still don't work... it give an error not exepected..i enclose again the screenshot...thanks a lot lestroso... :-[

madref

  • Hero Member
  • *****
  • Posts: 786
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Problem with this code.....can you help me??
« Reply #5 on: May 26, 2020, 04:29:47 pm »
Try this:
Code: Pascal  [Select][+][-]
  1. type
  2.   { TCheckInternet }
  3.   TCheckInternet = class(TFPHTTPClient)
  4.   public
  5.     class function Available(AServer: string='www.google.com'): boolean;
  6.   end;  // TCheckInternet
  7.  
  8.  
  9. { TCheckInternet }
  10. class function TCheckInternet.Available(AServer: string): boolean;
  11. var
  12.   c: TCheckInternet;
  13. begin
  14.   Result := False;
  15.   c:=TCheckInternet.Create(nil);
  16.   try
  17.     try
  18.       c.ConnectToServer(AServer,80);
  19.       Result := True;
  20.     except
  21.     end;
  22.   finally
  23.     c.Free;
  24.   end;
  25. end;     // TCheckInternet.Available    
  26.  


Code: Pascal  [Select][+][-]
  1.  
  2. function HasInternetConnection : boolean;
  3. begin
  4.   Result := TCheckInternet.Available();
  5. end;     // HasInternetConnection
  6.  
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 2.0.6 / FPC 3.0.4
Lazarus Trunc / FPC Trunc
Mac OS X Mojave

lestroso

  • Full Member
  • ***
  • Posts: 107
    • FaSasoftware
Re: Problem with this code.....can you help me??
« Reply #6 on: May 26, 2020, 06:03:17 pm »
Dear madref,..thanks alot for your code...but i can't reproduce your code in my software...it's a little complicated...there're no message window to see if internet connection work or not....than ks a lot in any way...Lestroso

lestroso

  • Full Member
  • ***
  • Posts: 107
    • FaSasoftware
Re: Problem with this code.....can you help me??
« Reply #7 on: May 26, 2020, 06:56:41 pm »
This code give me an error when push my button in the ide lazarus, but if i run my software as standalone....work without break or error....ok fine..Lestroso..


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  begin
  3.       with TFPHttpClient.Create(nil) do begin
  4.       try
  5.         s:= get('https://www.google.com');
  6.         if  s <> ('') then ShowMessage (copy(s,1,50))
  7.         except
  8.         on e:Exception do ShowMessage ('Please Connect To Internet!! ');
  9.       end;
  10.     end;
  11. end;
  12. end;
  13. end.
  14.                
  15.  

jamie

  • Hero Member
  • *****
  • Posts: 3096
Re: Problem with this code.....can you help me??
« Reply #8 on: May 26, 2020, 07:46:49 pm »
using "GET" is assumed that you are already connected..

Try using SimpleGet;

Code: Pascal  [Select][+][-]
  1.  S := FpHttpClient1.SimpleGet('https://www.google.com');
  2.  

I did that with component already on the form, precreated.
There could be some properties that need to be streamed in but you can try that locally too..

 SimpleGet does all the connection setup etc.

That works for me..
.
The only true wisdom is knowing you know nothing

madref

  • Hero Member
  • *****
  • Posts: 786
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Problem with this code.....can you help me??
« Reply #9 on: May 26, 2020, 08:30:16 pm »
Dear madref,..thanks alot for your code...but i can't reproduce your code in my software...it's a little complicated...there're no message window to see if internet connection work or not....than ks a lot in any way...Lestroso
That's easy...
Code: Pascal  [Select][+][-]
  1. if HasInternetConnection = false then showmessage ('No internet');
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 2.0.6 / FPC 3.0.4
Lazarus Trunc / FPC Trunc
Mac OS X Mojave

jamie

  • Hero Member
  • *****
  • Posts: 3096
Re: Problem with this code.....can you help me??
« Reply #10 on: May 26, 2020, 09:02:45 pm »
Dear madref,..thanks alot for your code...but i can't reproduce your code in my software...it's a little complicated...there're no message window to see if internet connection work or not....than ks a lot in any way...Lestroso
That's easy...
Code: Pascal  [Select][+][-]
  1. if HasInternetConnection = false then showmessage ('No internet');

True, I understand what you are saying but using just the GET before connecting and setting up the control I don't think is going to work so well, at least not before initial properties get configured..

 I use it via the SimpleGet and it works for me but I also placed the Client on the form so it was already created and initialized..

 Issuing a simple get means you need to specify some more stuff! ;)
The only true wisdom is knowing you know nothing

lestroso

  • Full Member
  • ***
  • Posts: 107
    • FaSasoftware
Re: Problem with this code.....can you help me??
« Reply #11 on: May 27, 2020, 11:13:53 am »
DEAR jamie,dear madref....

can you could more precise?? i can't replicate your software, can you be more clear please??i enclose here my software with your ideas....thanks lestroso :)

Code: Pascal  [Select][+][-]
  1. unit pro;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,fphttpclient;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.  
  19.   public
  20.     end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.    s: AnsiString;
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.Button1Click(Sender: TObject);
  32. begin
  33.   with TFPHttpClient.Create(nil) do begin
  34.   try
  35.     //s:= get('https://www.google.com');
  36.     S := FpHttpClient.SimpleGet('https://www.google.com');
  37.         if HasInternetConnection = false then showmessage ('No internet');
  38.     //if  s <> ('') then ShowMessage (copy(s,1,50))
  39.     //else ShowMessage ('Please Connect To Internet!!');
  40.   //except
  41.     //on e:Exception do ShowMessage ('Error connecting, message: '+e.Message);
  42.   //end;
  43. //end;
  44.  
  45. end;
  46.  
  47. end.
  48.  

Thaddy

  • Hero Member
  • *****
  • Posts: 10190
Re: Problem with this code.....can you help me??
« Reply #12 on: May 27, 2020, 12:09:18 pm »
Huh? did anybody notice the real culprit? Free What?
Lot's of code with little substance but there is a blatant bug:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.      begin
  3.   with TFPHttpClient.Create(nil) do begin
  4.       s:= get('https://www.google.com');
  5.      if  s <> ('')
  6.     then ShowMessage (copy(s,1,50))
  7.     else ShowMessage ('Please Connect To Internet!!'); // with block ends here...
  8. free;  // Huh? this is not in the with scope
  9.  end;
  10. end;
I am more like donkey than shrek

jamie

  • Hero Member
  • *****
  • Posts: 3096
Re: Problem with this code.....can you help me??
« Reply #13 on: May 27, 2020, 12:51:17 pm »
Huh? did anybody notice the real culprit? Free What?
Lot's of code with little substance but there is a blatant bug:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.      begin
  3.   with TFPHttpClient.Create(nil) do begin
  4.       s:= get('https://www.google.com');
  5.      if  s <> ('')
  6.     then ShowMessage (copy(s,1,50))
  7.     else ShowMessage ('Please Connect To Internet!!'); // with block ends here...
  8. free;  // Huh? this is not in the with scope
  9.  end;
  10. end;

And please point out the "Huh?"

 Free is within the scope....
 
Don't you see that, the object is created and entered within a BEGIN....END block...
Free is part of the created object...

Oh well...

Must be age, cause I can't see what you are seeing..

The only true wisdom is knowing you know nothing

bytebites

  • Sr. Member
  • ****
  • Posts: 281
Re: Problem with this code.....can you help me??
« Reply #14 on: May 27, 2020, 12:52:48 pm »
Proper indentation tells?
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   s: RawByteString;
  4. begin
  5.   with TFPHttpClient.Create(nil) do begin
  6.     s := get('https://www.google.com');
  7.     if s <> ('') then ShowMessage(copy(s, 1, 50))
  8.     else ShowMessage('Please Connect To Internet!!'); // with block ends here...
  9.     Free;  // Huh? this is not in the with scope
  10.   end;
  11. end;
  12.  

 

TinyPortal © 2005-2018