@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
req
if(CheckNetwork){
new GetVideoUrlAsyn().execute();
}else{
// Check internet connection
}
and
private boolean CheckNetwork() {
ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
return true;
} else {
return false;
}
}
You should use a broadcast receiver as mentioned in other SO answers, but since you have asked how to do this otherwise, here you go:
* Checks for an existing network connectivity
*
* @param context
* The {@link Context} which is needed to tap
* {@link Context#CONNECTIVITY_SERVICE}
* @return True if network connection is available
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
Will need additional permission to be specified in the manifest file
check internet before execute asyncTask
if(checkNetwork)
{
new GetVideoUrlAsyn().execute();
}
else
{
// do stuff
}
Check network connectivity before executing AsyncTask,
if(isNetworkAvailable(this))
{
new GetVideoUrlAsyn().execute();
}
and this is the method definition,
public boolean isNetworkAvailable(Context ctx)
{
ConnectivityManager cm = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()&& cm.getActiveNetworkInfo().isAvailable()&& cm.getActiveNetworkInfo().isConnected())
{
return true;
}
else
{
return false;
}
}
do something like this inside your method callhttppost_numvaluepair(URL, nameValuePairs)
of WebServiceCall
class,
HttpParams basicparams = new BasicHttpParams();
URI uri = new URI(url);
HttpPost method = new HttpPost(uri);
int timeoutConnection = 60000;//set your timeout period
HttpConnectionParams.setConnectionTimeout(basicparams,
timeoutConnection);
DefaultHttpClient client = new DefaultHttpClient(basicparams);
//and then rest of your code
Note: Don't forget to put the permission in manifest file,
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />