[SOLVED] jHttpClient redirections lazarus / 43-Android ---------------------------------------------------------------------- maurobio · 2023-07-05 21:49 Dear ALL, Can someone out there tell me if the jHttpClient LAMW component handles redirections automatically or how it can be done programmatically? When developing desktop applications in Free Pascal/Lazarus, one can use the fHttpClient component for handling redirections as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program dl_fphttp_c; {$mode delphi}{$ifdef windows}{$apptype console}{$endif} uses  classes,   fphttpclient,  openssl, { This implements the procedure InitSSLInterface }  opensslsockets; var  Client: TFPHttpClient; begin   { SSL initialization has to be done by hand here }  InitSSLInterface;   Client := TFPHttpClient.Create(nil);  try    { Allow redirections }    Client.AllowRedirect := true;    writeln(Client.Get('https://google.com/'));   finally    Client.Free;  end;end. Can something similar be achieved in LAMW? Thanks in advance! Best regards, ---------------------------------------------------------------------- maurobio · 2023-07-07 14:20 Dear ALL, A few days ago I opened this thread but have not yet got any answer to my problem. Therefore, to clarify the problem, I post here a more detailed description of it, as well as my complete test code. I want to get the summary text of Wikipedia articles and display it in a jTextView component. This is quite  easy using the jHttpClient component to get the article and fpjson / jsonparser to parse the returned content. It works fine (see attached screenshot), but just for some articles - as I wrote in my original post, seemingly jHttpClient does not handle redirections; therefore it will fail when a given search term redirects to an article. For example, if searching for "Vicia faba" (as shown in the screenshot) jHttpClient.Get() method will work fine, but if searching for "Loxodonta africana" (the African elephant) it will fail. And, last but not least, it also seems that the try/exception is never executed, because instead of displaying an error message, the app simply closes when jHttpClient encounters a redirection. Has someone a solution to this problem? It should be noticed that redirections are not uncommon in websites and the inability to handle them puts severe limits to the usefulness of the jHttpClient component. My complete source code for the test app is attached. With warmest regards, ---------------------------------------------------------------------- jmpessoa · 2023-07-08 20:04 Done! NEW! Now you can use "SetFollowRedirects"  method before "Get".... Thank you! ---------------------------------------------------------------------- maurobio · 2023-07-08 20:14 Hi, jmpessoa! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i NEW! Now you can use "SetFollowRedirects"  method before "Get".... Great! I am truly glad in contributing with my modest efforts to the improvement of this superb development tool which is LAMW! In fact, I had already found another solution to the problem of redirections, but that solution is limited to Wikipedia (consisting in simply calling another API) but yours is, obviously, much better because it is a general solution. Anyway, I will post my other solution here tomorrow, for the benefit of the learning developers. With warmest regards, ---------------------------------------------------------------------- maurobio · 2023-07-09 14:41 Dear ALL, As I mentioned in my previous reply to jmpessoa, I have found a simple solution for handling the redirection of Wikipedia pages. However, that is not a general solution and is limited to Wikipedia only - for a general solution, see the latest version of LAMW which now includes the handling of redirections for any page. Anyway, I am attaching my amended code to demonstrate my simple solution, just in case. It works - with one very strange exception which I would like to ask you for help in solving! The problem I found is not related to redirections, so I presume it would appear also in the new version of LAWM. Here is a description of the problem: if I search for the term "Vicia faba" (which is not redirected), it works fine. Likewise, searching for the term "Panthera leo" (which Wikipedia redirects to "Lion"), it also works fine. But searching for "Loxodonta africana" (which is redirected to "African bush elephant"), the app closes without further notice! In my code, I have added several checks which show that the problem is not related to redirections, but seems to be related to the text itself extracted from the JSON file returned by Wikipedia. However, I could not find anything different in this file, as compared to any other! Could someone please take a look at my test app to try and see what this strange (and very annoying) problem may be? Thanks in advance for any assistance you can provide! With warmest regards, ---------------------------------------------------------------------- Insid3Code · 2023-07-09 18:13 This is a very very quick check (need more investigation)... with the help of "logcat" I found this error: JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xa0 I changed the type from asString to AsUnicodeString  TextView2.Text := JsonData.FindPath('extract').AsUnicodeString; See Animated gif. https://i.imgur.com/fiwkgrk.gif ---------------------------------------------------------------------- maurobio · 2023-07-09 19:31 Hi, @Insid3Code! QuoteI changed the type from asString to AsUnicodeString I suspected that there was something wrong with the returned string; I had myself previously tried 'AnsiString' to no effect. By the way, how can I use the 'logcat' tool? Is it included in LAWM? Thanks a lot for taking time to solve this annoying 'elephant-sized' problem!  You are really a wizard! With warmest regards, ---------------------------------------------------------------------- jmpessoa · 2023-07-09 20:25 QuoteBy the way, how can I use the 'logcat' tool? REF > https://developer.android.com/tools/logcat Use > Connect your device to PC and then: C:\android\sdkr25\platform-tools>adb logcat -b main ---------------------------------------------------------------------- maurobio · 2023-07-09 20:46 Hi, @jmpessoa!!! Thank you very much!  With warmest regards, ---------------------------------------------------------------------- Mongkey · 2023-07-11 09:12 Hi maurobio, it seems you are national geographic man, lazarus has a lot scientific software  . Just try to port it to android, it would be great! https://wiki.freepascal.org/Portal:SciTech ---------------------------------------------------------------------- maurobio · 2023-07-11 20:50 Hi, @Mongkey!!! QuoteHi maurobio, it seems you are national geographic man I feel flattered! I surely would not mind working for the National Geographic Society! Quotelazarus has a lot scientific software Indeed, scientific computing (especially biodiversity informatics) is my main field of expertise, and I surely hope to contribute to the many scientific applications already developed in Lazarus. QuoteJust try to port it to android, it would be great! Yes, I am working on this right now! Some of my already available desktop scientific applications written in Lazarus are just in the process of being ported to Android using LAMW. Thank you very much! With warmest regards, ---------------------------------------------------------------------- Mongkey · 2023-07-12 15:58 i did micropropagation and protein isolation, but right now i forget everything , drowning into simple computing things.