lib/android: implement http_request, using the Java Apache lib
[nit.git] / lib / android / http_request.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Android implementation of `app:http_request`
16 module http_request is
17 android_manifest """<uses-permission android:name="android.permission.INTERNET" />"""
18 end
19
20 intrude import app::http_request
21 import ui
22
23 in "Java" `{
24 import org.apache.http.client.methods.HttpGet;
25 import org.apache.http.impl.client.DefaultHttpClient;
26 import org.apache.http.HttpResponse;
27 import org.apache.http.HttpStatus;
28 import org.apache.http.StatusLine;
29 import java.io.ByteArrayOutputStream;
30 `}
31
32 redef class App
33 redef fun run_on_ui_thread(task) do app.native_activity.run_on_ui_thread task
34 end
35
36 redef class Text
37
38 redef fun http_get
39 do
40 jni_env.push_local_frame 8
41 var juri = self.to_java_string
42 var jrep = java_http_get(juri)
43
44 assert not jrep.is_java_null
45
46 var res
47 if jrep.is_exception then
48 jrep = jrep.as_exception
49 res = new HttpRequestResult(null, new IOError(jrep.message.to_s))
50 else if jrep.is_http_response then
51 jrep = jrep.as_http_response
52 res = new HttpRequestResult(jrep.content.to_s, null, jrep.status_code)
53 else abort
54
55 jni_env.pop_local_frame
56 return res
57 end
58 end
59
60 redef class AsyncHttpRequest
61
62 redef fun main
63 do
64 var res = super
65 jvm.detach_current_thread
66 return res
67 end
68 end
69
70 redef class JavaObject
71 private fun is_exception: Bool in "Java" `{ return self instanceof Exception; `}
72 private fun as_exception: JavaException in "Java" `{ return (Exception)self; `}
73
74 private fun is_http_response: Bool in "Java" `{ return self instanceof HttpResponse; `}
75 private fun as_http_response: JavaHttpResponse in "Java" `{ return (HttpResponse)self; `}
76 end
77
78 private fun java_http_get(uri: JavaString): JavaObject in "Java" `{
79 try {
80 DefaultHttpClient client = new DefaultHttpClient();
81 HttpGet get = new HttpGet(uri);
82 return client.execute(get);
83 } catch (Exception ex) {
84 return ex;
85 }
86 `}
87
88 private extern class JavaHttpResponse in "Java" `{ org.apache.http.HttpResponse `}
89 super JavaObject
90
91 fun status_code: Int in "Java" `{ return self.getStatusLine().getStatusCode(); `}
92
93 fun content: JavaString in "Java" `{
94 try {
95 ByteArrayOutputStream out = new ByteArrayOutputStream();
96 self.getEntity().writeTo(out);
97 out.close();
98 return out.toString();
99 } catch (Exception ex) {
100 ex.printStackTrace();
101 return "";
102 }
103 `}
104 end