I need to verify the internet connection while pressing log-out button in my app and it must show a toast message "connection error" if connection is not present and must not go to the login page.
Following is the error code that i had received in the console window while running the emulator.
Android Runtime Errors: Fatal Exception: thread -85
java.lang.RuntimeException: Can't create handler inside thread.
I had created a class called "appstatus.java" and used it and working fine with the login page but while i try to use same if else condition with the "mainactivity.java" where logout button is present. its not working and app crashes. below is my code:-
@Override public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (AppStatus.getInstance(getBaseContext()).isOnline(getBaseContext())) {
//
Log.v("driver-id from logout:",""+drv_id);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(ServerConnection.ip+"LogoutFlag.jsp");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
Log.v("drv id from logout:",""+drv_id);
nameValuePairs.add(new BasicNameValuePair("driver_id", ""+drv_id));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httppost, responseHandler);
String reverseString = response.trim();
Log.v("Response device id from logout",reverseString);
}
catch (Exception e) {
Log.e("logout error:",e.toString());
}
}
else {
Toast.makeText(getBaseContext(), "Connection Lost", Toast.LENGTH_SHORT).show();
}
}
}).start();
I had used same if, else condition to check internet connectivity on the login page and is working fine. here the only difference i notice is a try catch method is used.
My question is:-
Why the app is crashing with above code?
How to rewrite the code avoiding errors for the above mentioned cause?
Any piece of code is highly appreciated and thanks in advance.
Try this way,hope this will help you to solve your problem
Follow few steps to check internet connection and make code without crashes
Step 1. make this method, for checking connection is available or not
void isConnectedToInternet() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null)
greenFlag();
else
new AlertDialog.Builder(activity)
.setTitle("Network connection error !")
.setMessage("Please check internet settings to continue.")
.setNegativeButton("Continue",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
isConnectedToInternet();
}
}).show();
}
Step 2. Run this method on oncreate,
isConnectedToInternet();
Step 3. In this mehtod you need to put all data related to webservices and others task,
void greenFlag() {
// Anything you want to do here
}
thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.