mnet - crash if no internet connection
Monkey Targets Forums/Android/mnet - crash if no internet connection
| ||
| Hi all, I've had a look at mnet for the purpose of using the httpget command, it works fine apart from one problem I'm having. If I turn off the internet connection (airplane mode/mobile network off) on my phone then I simply get a crash / force close on my phone. Is there some way of checking for a valid internet connection beforehand or at least returning a 'no internet connection' response of some sort. Thanks, Matt |
| ||
Something like this:
public static boolean isNetworkConnected( Context c )
{
boolean wifiConnected = false;
boolean mobileConnected = false;
ConnectivityManager cm = (ConnectivityManager)c.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo)
{
if ("WIFI".equalsIgnoreCase(ni.getTypeName()))
if (ni.isConnected())
wifiConnected = true;
if ("MOBILE".equalsIgnoreCase(ni.getTypeName()))
if (ni.isConnected())
mobileConnected = true;
}
return wifiConnected || mobileConnected;
}
And you need these permissions in the manifest: <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> |
| ||
| I ran in the same problem here: http://www.monkeycoder.co.nz/Community/posts.php?topic=1294& No one answered, so I changed the mnet.android.java, that it returns a "failed" string, if no connection is possible. I can give no guarantee, but for my purposes it works so far.
// 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.
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 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 ){
return "failed"; ' changed by myself! *******************************
}
{
}
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 )
{
}
return sb.toString();
}
}
|
| ||
| Thanks all, I added the following code to the mnet module: (with the other import statements:) import android.net.ConnectivityManager;
(inside mnet itself)
public static boolean isOnline()
{
boolean var = false;
ConnectivityManager cm = (ConnectivityManager)MonkeyGame.activity.getSystemService(Context.CONNECTIVITY_SERVICE);
if ( cm.getActiveNetworkInfo() != null )
{
var = true;
}
return var;
}
then use an 'if IsOnline' statement inside the HttpGet method. Thanks therevills - I almost had it right - I kept getting an 'cannot find symobl' error because I was not using "MonkeyGame.activity." before getSystemService... this is a method I found on slashdot but it didn't work if I pasted it just as found... |
| ||
| Interesting...it works if I'm using my mobile network, it also works if there is no internet connection (airplane mode or mobile network switched off) - I am able to handle the error and continue. If I connect to my work's WiFi network it still force closes...not sure why...but when I'm connected to my work's WiFi network I have a connection according to my phone but I cannot browse the internet through the browser that comes with the phone either... Hmmm - I wonder if I restrict the internet connectivity to only when the active connection is a mobile network if that would be acceptable? I'm not sure at which point it is falling over though - I think it establishes that there is a connection, and therefore "IsOnline" returns true, but for some reason it is unable to connect to the url with the httpget command... |
| ||
| Actually - using Volker's method above as well seems to solve the problem. So now I check for an internet connection first, and then output a response in the catch section if it fails. |
| ||
| Will add that to MNet, thanks guys! :) |
| ||
| When and where is the latest version of MNet available? Version at the Google Code page seems old. |
| ||
| The download version is a bit old, I'll create a new one. I'll refactore the whole lib as well and I'm still in the process to add sockets. |
| ||
| @Xaron Thank you for this nice tool... did you meanwhile update your mnet? I still get this error, when connection runs out of time... Tested with android smartphone and LAN and Xampp Server: When I access "http://192.168.2.105/VoteServer/index.php" it workes fine, when I access "http://192.168.2.105" it crashes! Or Is there any chance to do a (Monkey-sided) workaround to the returned empty string? It looks like Monkey cannot handle or check such strings: [monkeycode]Result=Server.Get( Adress, 3000 ) If Result=NULL[/monkeycode] gives... "Cannot convert von {NULL} to String" and this.... [monkeycode]Result=Server.Get( Adress, 3000 ) If Result=""[/monkeycode] gives a crash... "Monkey Runtime Error : Attempt to acess null object" And I have the feeling, that this code does not wait for 3sec until it crashs... Third: I try to access from my smartphone via LAN to Xampp Apache on 192.168.2.100. With the smartphone browser it works and I get the Xampp "root" warning, with the code above I get immediately a crash. |