mnet extended with asynchronous HttpGet
Monkey Targets Forums/Android/mnet extended with asynchronous HttpGet
| ||
Hi all, I have extended the mnet library by Martin Leidel aka Xaron, and added an asyncHttpGet function, so that the whole httpget runs inside a thread. In this way, we don't have to wait for the response. This addition should not break any code that uses the simple httpget method. mnet.android.java: // Android MNet runtime. // // Copyright 2011 Martin Leidel, all rights reserved. // // This software is provided 'as-is', without any express or implied // warranty. In no event will the authors be held liable for any damages // arising from the use of this software. // // Permission is granted to anyone to use this software for any purpose, // including commercial applications, and to alter it and redistribute it // freely, subject to the following restrictions: // // 1. The origin of this software must not be misrepresented; you must not // claim that you wrote the original software. If you use this software // in a product, an acknowledgment in the product documentation would be // appreciated but is not required. // 2. Altered source versions must be plainly marked as such, and must not be // misrepresented as being the original software. // 3. This notice may not be removed or altered from any source distribution. // The functions asyncHttpGet and getResult extend this Android MNet runtime in order to provide asynchronous HttpGet. // Author of the async extension: Sergio Marcello - semar import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; class mnet { public static String result = ""; public static String getResult() { return result; } public static String asyncHttpGet(String url, int timeoutConnection, int timeoutSocket) { result = ""; httpThread ht = new httpThread(url, timeoutConnection, timeoutSocket); ht.start(); return "started"; } public static String HttpGet( String url, int timeoutConnection, int timeoutSocket ) { HttpURLConnection con = null; InputStream is = null; try { URL connectToURL = new URL( url ); con = ( HttpURLConnection )connectToURL.openConnection(); con.setReadTimeout( timeoutSocket ); con.setConnectTimeout( timeoutConnection ); con.setRequestMethod( "GET" ); con.setDoInput( true ); // Start the query con.connect(); is = con.getInputStream(); } catch( IOException e ) { mnet.result = "ERR_HttpGet " + e.getMessage(); } return convertStreamToString( is ); } public static String convertStreamToString( InputStream is ) { StringBuilder sb = new StringBuilder(); try { BufferedReader reader = new BufferedReader( new InputStreamReader( is ) ); String line = null; while( ( line = reader.readLine() ) != null ) { sb.append( line + "\n" ); } is.close(); } catch( IOException e ) { mnet.result = "ERR_convertStreamToString " + e.getMessage(); } return sb.toString(); } } class httpThread extends Thread { String url; int timeoutConnection; int timeoutSocket; httpThread(String url, int timeoutConnection, int timeoutSocket) { this.url = url; this.timeoutConnection = timeoutConnection; this.timeoutSocket = timeoutSocket; } public void run() { //System.out.println(mnet.asyncHttpGet(url, timeoutConnection, timeoutSocket)); try { mnet.result = mnet.HttpGet(url, timeoutConnection, timeoutSocket); } catch (Exception e) { mnet.result = "ERR_run " + e.getMessage(); } } } mnet.monkey: ' Copyright 2011 Martin Leidel ' ' This software is provided 'as-is', without any express or implied ' warranty. In no event will the authors be held liable for any damages ' arising from the use of this software. ' ' Permission is granted to anyone to use this software for any purpose, ' including commercial applications, and to alter it and redistribute it ' freely, subject to the following restrictions: ' ' 1. The origin of this software must not be misrepresented; you must not ' claim that you wrote the original software. If you use this software ' in a product, an acknowledgment in the product documentation would be ' appreciated but is not required. ' 2. Altered source versions must be plainly marked as such, and must not be ' misrepresented as being the original software. ' 3. This notice may not be removed or altered from any source distribution. ' The functions asyncHttpGet and getResult extend this Android MNet runtime in order to provide asynchronous HttpGet. ' Author of the async extension: Sergio Marcello - semar #If TARGET <> "android" And TARGET <> "html5" And TARGET <> "ios" #Error "MNet is not currently available for target '${TARGET}'." #End Import "native/mnet.${TARGET}.${LANG}" Extern #IF TARGET = "html5" Or TARGET = "ios" Then Function HttpGet:String( url:String, timeoutConnection:Int = 3000, timeoutSocket:Int = 5000 ) = "mnetHttpGet" #ELSE Function HttpGet:String( url:String, timeoutConnection:Int = 3000, timeoutSocket:Int = 5000 ) = "mnet.HttpGet" Function asyncHttpGet:String( url:String, timeoutConnection:Int = 3000, timeoutSocket:Int = 5000 ) = "mnet.asyncHttpGet" Function getResult:String() = "mnet.getResult" #ENDIF #IF TARGET = "html5" Then Function HttpPost:String( url:String, params:String, timeoutConnection:Int = 3000, timeoutSocket:Int = 5000 ) = "mnetHttpPost" #End Example of usage (monkey) ' Copyright 2011 Martin Leidel ' ' This software is provided 'as-is', without any express or implied ' warranty. In no event will the authors be held liable for any damages ' arising from the use of this software. ' ' Permission is granted to anyone to use this software for any purpose, ' including commercial applications, and to alter it and redistribute it ' freely, subject to the following restrictions: ' ' 1. The origin of this software must not be misrepresented; you must not ' claim that you wrote the original software. If you use this software ' in a product, an acknowledgment in the product documentation would be ' appreciated but is not required. ' 2. Altered source versions must be plainly marked as such, and must not be ' misrepresented as being the original software. ' 3. This notice may not be removed or altered from any source distribution. Strict Import mojo Import mnet Global testApp:HttpGet Global conta:Int Global status:Int = 0 Function Main:Int() testApp = New HttpGet() Return 0 End Function Class HttpGet Extends App Field httpResult:String Field threadStatus:String = "stopped" Method OnCreate:Int() SetUpdateRate(30) Return 0 End Method Method OnUpdate:Int() If TouchHit(0) status = 0 Endif If TouchHit(1) threadStatus = mnet.asyncHttpGet( "http://www.monkeycoder.co.nz", 3000 ) status = 1 Endif Return 0 End Method Method OnSuspend:Int() Return 0 End Method Method OnResume:Int() Return 0 End Method Method OnRender:Int() Cls Local sc:Float = 2 Scale( sc, sc ) DrawText( "one finger = clear screen", 10, 10 ) DrawText( "two fingers = start asyncHttp", 10, 20 ) DrawText( "threadStatus: " + threadStatus, 10, 30 ) If status = 1 httpResult = mnet.getResult() If httpResult = "" Then conta += 1 conta = (conta Mod 50) If (conta < 25) Then DrawText( "PLEASE WAIT..", 10, 50 ) Endif Else Local row:Int = 70 For Local value:String = Eachin httpResult.Split( "," ) 'Print value row += 10 If value.Length() > 1 DrawText( "rows: " + value, 10, row ) Endif Next httpResult = "" threadStatus = "stopped" Endif Endif Return 0 End Method End Class Regards, Sergio. |
| ||
Yay great. Will check that in! Do you want to be a co author? That way you could check in those changes as well. |
| ||
Well, why not :) Waiting for further instructions.. Sergio. |
| ||
When will this get checked in? |
| ||
Oh my, completely forgot about this. I'm sorry! It's checked in now. Will have to build a new zip as well but I'll wait till I have added sockets. |