Reputation: 464
hello sir in login page i am validating username and password from server database. if net is there means my application is working properly. if i disconnect internet means my application run means it show error application was not responding. how to eliminate this type of error
i try this code
if(name.equals("") || pass.equals(""))
{
Toast.makeText(Main.this, "Please Enter Username and Password", Toast.LENGTH_LONG).show();
}
else
{
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("server url/login.php");
// Add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim()));
nameValuePairs.add(new BasicNameValuePair("Password", pass.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
data = new byte[256];
buffer = new StringBuffer();
int len = 0;
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len));
}
inputStream.close();
}
catch (IOException e) {
System.out.println(e);
//alertDialog.cancel();
}
if(buffer.charAt(0)=='Y')
{
Intent intent=new Intent(getApplicationContext(),ManagerHandset.class);
startActivity(intent);
}
else
{
Toast.makeText(Main.this, "Invalid Username or password", Toast.LENGTH_LONG).show();
}
}
}
});
if i want disconnect my net means how to show alert net is not available
Upvotes: 0
Views: 1531
Reputation: 875
check the internet connection if net is avaible or not..
private boolean isOnline()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected())
{
return true;
}
else
{
return false;
}
}
Upvotes: 0
Reputation: 9020
You can check your internet connection through this type of function:
public boolean isNetworkAvailable(Context context) {
boolean value = false;
ConnectivityManager connec = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
value = true;
}
// Log.d ("1", Boolean.toString(value) );
return value;
}
Remember you have added following permissions in your Manifest file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Edit
if (isNetworkAvailable(getApplicationContext()))
{
// Do whatever you want to do
}
else{
new AlertDialog.Builder(YourActivitName.this)
.setTitle("Error")
.setMessage("Your Internet Connection is not available at the moment. Please try again later.")
.setPositiveButton(android.R.string.ok, null)
.show();
}
Hope this will work for you...
Upvotes: 1
Reputation: 27284
The check of the network status before stating a web call is a good habit and all the methods explained in the other responses are right. Link to the android doc.
If your application is not responding when the network is disconnected, it means you are stating your HTTP request on the main thread. You should NEVER do that.
Even if it works fine when the network is up over a Wifi connexion, you will face the same "not responding" error over a slow EDGE connection.
To make the HTTP request outside the main thread you should use an AsyncTask, the principle is explained in the android doc.
Upvotes: 0
Reputation: 9995
You can use something like this:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
If it returns true
continue with your normal code else show a message.. also in your manifest file add
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Upvotes: 0