android & benitlux: use NitObject in clients
[nit.git] / lib / android / http_request.nit
index d7427c3..b527cde 100644 (file)
@@ -30,7 +30,15 @@ in "Java" `{
 `}
 
 redef class App
-       redef fun run_on_ui_thread(task) do app.native_activity.run_on_ui_thread task
+       redef fun run_on_ui_thread(task)
+       do
+               if app.activities.not_empty then
+                       app.native_activity.run_on_ui_thread task
+               else
+                       # There is no UI, it must be a service, run on the caller thread
+                       task.main
+               end
+       end
 end
 
 redef class Text
@@ -46,7 +54,23 @@ redef class Text
                var res
                if jrep.is_exception then
                        jrep = jrep.as_exception
-                       res = new HttpRequestResult(null, new IOError(jrep.message.to_s))
+
+                       # If the top exception doesn't have a message, get it from its causes.
+                       var msg = null
+                       var cause: JavaThrowable = jrep
+                       loop
+                               var jmsg = cause.message
+                               if not jmsg.is_java_null then
+                                       msg = jmsg.to_s
+                                       break
+                               else
+                                       cause = cause.cause
+                                       if cause.is_java_null then break
+                               end
+                       end
+                       if msg == null then msg = jrep.to_s
+
+                       res = new HttpRequestResult(null, new IOError(msg))
                else if jrep.is_http_response then
                        jrep = jrep.as_http_response
                        res = new HttpRequestResult(jrep.content.to_s, null, jrep.status_code)
@@ -81,6 +105,7 @@ private fun java_http_get(uri: JavaString): JavaObject in "Java" `{
                HttpGet get = new HttpGet(uri);
                return client.execute(get);
        } catch (Exception ex) {
+               ex.printStackTrace();
                return ex;
        }
 `}
@@ -102,3 +127,8 @@ private extern class JavaHttpResponse in "Java" `{ org.apache.http.HttpResponse
                }
        `}
 end
+
+# Force linearization of print
+#
+# TODO prioritize `android::log`
+redef fun print(object) do super