Jump to content
Sign in to follow this  
Fabian1648

[Android][Rio] How catch timeout REST error?

Recommended Posts

Hi,

 

In an Android application, how can we catch the exception that is triggered when we make a REST request that remains unanswered.
 

The following code does not work. When we launch the REST request, the app freezes until the REST timeout is exceeded and then Android displays a message "the app does not respond"

 

try
 //Blabla...
except
 on E: Exception do
  begin
   //blabla...
  end;
end;

 

Share this post


Link to post

If you set TRESTClient's SynchronizedEvents property to False, you can do this directly:

  // FRequest: TRESTRequest
  // FClient: TRESTClient
  // FCanRaiseException: Boolean = {$IFDEF DEBUG}True{$ELSE}False{$ENDIF}

function TryRequest: Boolean
begin
  FClient.SynchronizedEvents := False;
  FRequest.Client := FClient;
  try
    FRequest.Execute;
  except
    on E: ERESTException do
    begin
      if FCanRaiseException then
        Exception.RaiseOuterException(Exception.Create('Service or connection failed'))
      else
        Exit(False);
    end;
  end;
  Result := True;
end;

This code is blocking, so it must be executed in a task in the background, and communicating with the UI through a queue.

Just a question, what rest api do you is integrating? You may be able to make this integration faster with the Refit library: viniciusfbb/ipub-refit: Simple way to consume REST services (github.com)

Edited by vfbb
  • Like 1
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×