Recent

Author Topic: LAMW and TThread  (Read 1215 times)

Robert Gilland

  • Full Member
  • ***
  • Posts: 146
Re: LAMW and TThread
« Reply #15 on: June 25, 2019, 08:53:11 am »
here is the core code:

Code: Pascal  [Select]
  1. {Hint: save all files to location: C:\adt32\eclipse\workspace\asyncdemo\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Laz_And_Controls, AndroidWidget, user_service_intf,
  10.   user_service_intf_proxy, And_Log_h;
  11.  
  12. type
  13.  
  14.   { TAndroidModule1 }
  15.  
  16.   TAndroidModule1 = class(jForm)
  17.       jAsyncTask1: jAsyncTask;
  18.       jButton1: jButton;
  19.       jDialogProgress1: jDialogProgress;
  20.       jEditText1: jEditText;
  21.       jListView1: jListView;
  22.       procedure AndroidModule1Create(Sender: TObject);
  23.       procedure AndroidModule1Destroy(Sender: TObject);
  24.       procedure jAsyncTask1DoInBackground(Sender: TObject; progress: Integer;
  25.         out keepInBackground: boolean);
  26.       procedure jAsyncTask1PostExecute(Sender: TObject; progress: integer);
  27.       procedure jAsyncTask1PreExecute(Sender: TObject; out
  28.         startProgress: integer);
  29.       procedure jButton1Click(Sender: TObject);
  30.     private
  31.       {private declarations}
  32.       procedure LogMessage( psMsg : String );
  33.       procedure   RunGetUsers;
  34.       function CreateUserProxy : UserService;
  35.     public
  36.       {public declarations}
  37.       slUsers : TStringList;
  38.   end;
  39.  
  40. var
  41.   AndroidModule1: TAndroidModule1;
  42.  
  43. implementation
  44.  
  45. {$R *.lfm}
  46.  
  47. uses soap_formatter, json_formatter, xmlrpc_formatter, fpc_http_protocol, service_intf;
  48.  
  49. { TAndroidModule1 }
  50.  
  51.  
  52. procedure TAndroidModule1.jAsyncTask1DoInBackground(Sender: TObject; progress: Integer; out keepInBackground: boolean);
  53. begin
  54.   RunGetUsers;
  55.   keepInBackground:= False;
  56. end;
  57.  
  58. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  59. begin
  60.   slUsers := tStringList.Create;
  61. end;
  62.  
  63. procedure TAndroidModule1.AndroidModule1Destroy(Sender: TObject);
  64. begin
  65.   slUsers.Free;
  66. end;
  67.  
  68. procedure TAndroidModule1.jAsyncTask1PostExecute(Sender: TObject; progress: integer);
  69. begin
  70.   LogMessage('Dialog Stop');
  71.   jDialogProgress1.Stop;
  72.   LogMessage('Async Stop');
  73.   jAsyncTask1.Done;
  74.   LogMessage('List View Assign');
  75.   jListView1.Items.Assign(slUsers);
  76.   ShowMessage(slUsers.text);
  77. end;
  78.  
  79. procedure TAndroidModule1.jAsyncTask1PreExecute(Sender: TObject; out startProgress: integer);
  80. begin
  81.    startProgress:= 0;  //out param !
  82.    LogMessage('Dialog Start');
  83.    jDialogProgress1.Start; //or Show
  84. end;
  85.  
  86.  
  87.  
  88.  
  89. procedure TAndroidModule1.jButton1Click(Sender: TObject);
  90. begin
  91.  
  92.    if not jAsyncTask1.Running then
  93.    begin
  94.      //ShowMessage('Execute ... ');
  95.      jAsyncTask1.Execute
  96.    end
  97.   else
  98.      ShowMessage('Running...');
  99. end;
  100.  
  101. procedure TAndroidModule1.LogMessage(psMsg: String);
  102. begin
  103.   __android_log_write(ANDROID_LOG_INFO,PChar(Text),PChar(psMsg));
  104. end;
  105.  
  106. procedure TAndroidModule1.RunGetUsers;
  107. var
  108.   lpUserService : UserService;
  109.   loUserArray   : TUserArray;
  110.   loUser        : TUser;
  111.   i             : Integer;
  112.  
  113. begin
  114.   LogMessage('Create proxy');
  115.   lpUserService := CreateUserProxy;
  116.   LogMessage('proxy Get List');
  117.   loUserArray   := lpUserService.GetList();
  118.   slUsers.Clear;
  119.   LogMessage('List Length ' + IntToStr(loUserArray.Length));
  120.   for i := 0 to loUserArray.Length-1 do
  121.     begin
  122.       loUser := loUserArray[i];
  123.       slUsers.Add( loUser.UserName );
  124.     end;
  125. end;
  126.  
  127. function TAndroidModule1.CreateUserProxy: UserService;
  128. var
  129.   buffsimplecall, buffTransport, buffFormat : string;
  130.  
  131.   function GetServiceURL( WebServiceAddress : String ):String;
  132.   begin
  133.     Result := WebServiceAddress   + '/services/UserService';
  134.   end;
  135.  
  136. begin
  137.   buffFormat     := 'json';
  138.   buffsimplecall := 'http:Address=' + GetServiceURL(jEditText1.Text) + '/?format=' + buffFormat;
  139.   buffTransport  := buffsimplecall;
  140.   buffFormat := buffFormat + ':';
  141.   LogMessage(buffTransport);
  142.   LogMessage(buffFormat);
  143.   Result     := TUserService_Proxy.Create( 'UserService',  buffFormat,  buffTransport  );
  144. end;
  145.  
  146.  
  147. end.
  148.