====== Differences ====== This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
script_apicomm [2015/05/17 20:15] jappie created |
script_apicomm [2015/05/17 20:29] (current) jappie |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== About the script ====== | ===== About the script ====== | ||
| - | * Purpose : Load multiple scripts in the right order. | + | * Purpose : Generic internet-API communication. |
| - | * Author : Jappie Toutenhoofd (https://plus.google.com/+JappieToutenhoofd) | + | * binds function APIcomm to .self |
| - | * Link : https://plus.google.com/+JappieToutenhoofd/posts/ | + | * in : URL string |
| - | + | * out: Json stringified | |
| - | ===== Use of the script ====== | + | * Author : Jappie Toutenhoofd (https://plus.google.com/+JappieToutenhoofd) |
| - | * Set this script on the load-event of your Desktop. | + | * Link : https://plus.google.com/+JappieToutenhoofd/posts/ |
| - | * Adjust the last lines to your needs. | + | |
| ====== Script code ====== | ====== Script code ====== | ||
| - | ======= Loader-1 ======= | + | ======= APIcomm ======= |
| <code> | <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> | </code> | ||