User Tools

Site Tools


script_apicomm

===== About the script ====== * Purpose : Generic internet-API communication. * binds function APIcomm to .self * in : URL string * out: Json stringified * Author : Jappie Toutenhoofd (https://plus.google.com/+JappieToutenhoofd) * Link : https://plus.google.com/+JappieToutenhoofd/posts/ ====== Script code ====== ======= APIcomm ======= <code> LL.bindClass("android.os.StrictMode"); var policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); LL.bindClass("java.net.URL"); LL.bindClass("java.net.HttpURLConnection"); LL.bindClass("java.io.OutputStream"); LL.bindClass("java.io.InputStream"); LL.bindClass("java.io.BufferedInputStream"); LL.bindClass("java.io.BufferedReader"); LL.bindClass("java.io.InputStreamReader"); LL.bindClass("java.lang.StringBuilder"); self.APIcomm = function(myURL) { try{ var Conn = new URL( encodeURI(myURL) ).openConnection(); Conn.setConnectTimeout(400); var Cin = new BufferedInputStream(Conn.getInputStream()); } catch(Exception ) { alert("No Internet or site connection!"); return; } var rslt = JSON.parse(readStream(Cin)); Cin.close(); Conn.disconnect(); return( JSON.stringify(rslt)); } function readStream(ISin) { var reader = null; reader = new BufferedReader(new InputStreamReader(ISin, "UTF-8" )); var bld = new StringBuilder() ; var line = ""; while ( (line = reader.readLine()) != null) { bld.append(line); } reader.close(); return bld.toString(); } </code>

script_apicomm.txt · Last modified: 2015/05/17 20:29 by jappie