Android app cant call any service method when running as a service
definition (i check this code runs ok in the service)
method DataAccess.doInitComponents();
begin
try
self.readPreferences();
self.loadBriefcase;
if (aesEnvelope = nil) then
begin
aesEnvelope := new AesEncryptionEnvelope(adapter_aesPassword);
aesEnvelope.Enabled := true;
android.util.Log.i(TAG, 'DataAccess.doInitComponents created envelope');
end;
if (fRemoteDataAdapter = nil) then
begin
fRemoteDataAdapter := new RemoteDataAdapter(java.net.URI.&create(adapter_serverUrl), adapter_dataService, adapter_loginService);
fRemoteDataAdapter.Message.Envelopes.put('AES', aesEnvelope);
fRemoteDataAdapter.DynamicSelect := true;
HttpClientChannel(fRemoteDataAdapter.ClientChannel).Timeout := 5000;
android.util.Log.i(TAG, 'DataAccess.doInitComponents create fRemoteDataAdapter');
end;
// 20/09/2017 creo data service
if (fDataService = nil) then
begin
android.util.Log.i(TAG, 'NewLibrary.DataService_Proxy adapter_serverUrl ' + adapter_serverUrl);
fDataService := new NewLibrary.DataService_Proxy(java.net.URI.&create(adapter_serverUrl));
//(fDataService.ProxyMessage as BinMessage).UseCompression := true;
(fDataService.ProxyClientChannel as HttpClientChannel).Timeout := 5000;
(fDataService.ProxyMessage as BinMessage).Envelopes.put('AES', aesEnvelope);
fDataService.ProxyClientChannel.ChannelCallback := new interface ClientChannel.IClientChannelCallback(
clientChannelNeedsLogin := method(anException: Exception)
begin
android.util.Log.i(TAG, 'DataAccess.doInitComponents SessionNotFoundException was thrown');
System.out.println("SessionNotFoundException was thrown");
exit true;
end,
requestFailWithException := method(anException: Exception)
begin
android.util.Log.i(TAG, 'DataService requestFailWithException ' + anException.Message);
doMessageShow('DataService requestFailWithException ' + anException.Message, false);
System.out.println(anException.Message);
end
);
android.util.Log.i(TAG, 'DataAccess.doInitComponents created fDataService');
end;
except
on e: Exception do
begin
doMessageShow('DataModule.diInitComponents error ' + e.Message, false);
end
end;
android.util.Log.i(TAG, 'DataAccess.doInitComponents end');
end;
call to service method
try
ServerDateTime := fDataService.getDateTime(aVersionCode);
aDateTimeProvider := TProveedorFecha.TProveedorFecha_Servidor;
result := True;
except
on e: Exception do
begin
doMessageShow('Can´t contact server.' + e.Message, false);
end;
end;
returns this error message
Can´t contact server. string arg == null
Same problem when trying to applyupdates to briefcase.
try
self.fRemoteDataAdapter.applyChanges(self.data.fBitacoraEntidad);
enviook := true;
except
on e: Exception do
begin
doMessageShow('Trying to send Briefcase error :'+ e.Message, true);
end;
end;
e.Message show no text
Trying to send Briefcase error:
As i say, when running the app when an activity it runs ok but when running in background as a service the only thing dont work is send briefcase and calling server. Even Internet check works fine.
...
on e: Exception do begin
var writer := new java.io.StringWriter();
var printWriter := new java.io.PrintWriter(writer);
e.printStackTrace(printWriter);
printWriter.flush();
var stackTrace := e.Message + " -- " + writer.toString();
doMessageShow(stackTrace, true);
end;
When the app is running like a normal app im caling the same code after a button click, no async call at all. I bet is in the MAIN or UI thread. And works perfect!
Then, when running as a service it fails. I read the document, understand the idea, but still dont see how can that work without any special async call code, just a simple call on the main thread (or UI thread as the document name) but dont work when running in background as a service? If remobjects is implementing internally some async call i have no idea. In any case still dont explain why called from main UI thread it works.
so it will first check that anException.Message is not null and only after that perform the rest of the logging using its value? It could be that the original exception is hidden behind another one.
F.e. you could try to use the following code:
var message := coalesce(anException.Message, exception.Class.TypeName);
The problem is the call in the main thread as @mh and you pointed. I already solved the aplly updates problem putting the call in an async task. Just need to solve the getTime in the same way i think.
Still not get why it works as an standard app… Anyway, thanks a lot to both for the amazing help!