hi this is my helper class where i check internal connection and xml paersing and use this class to another activity the problem is when server connected is working fine but when server not responding or invalid input code is blast stop unexpectedly i find out asyntask to resolve this isse but my problem is how can i used AsyncTask in this code? or how to show toast message if server not responding error message Connection Error aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa is show on log but not show on toast what do i do so my application not blast when server not respond? any idea?
public class AgAppHelperMethods {
private static final String LOG_TAG = null;
private static AgAppHelperMethods instance = null;
public static String varMobileNo;
public static String varPinNo;
String[][] xmlRespone = null;
public static String getUrl() {
String url = "https://demo.accessgroup.mobi/";
return url;
}
public static String[][] AgAppXMLParser(String parUrl) {
String _node, _element;
String[][] xmlRespone = null;
try {
String url = AgAppHelperMethods.getUrl() + parUrl;
URL finalUrl = new URL(url);
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(finalUrl.openStream()));
doc.getDocumentElement().normalize();
NodeList list = doc.getElementsByTagName("*");
_node = new String();
_element = new String();
xmlRespone = new String[list.getLength()][2];
for (int i = 0; i < list.getLength(); i++) {
Node value = list.item(i).getChildNodes().item(0);
_node = list.item(i).getNodeName();
_element = value.getNodeValue();
xmlRespone[i][0] = _node;
xmlRespone[i][1] = _element;
} //end for
} //end try
catch (Exception e) {
// Toast.makeText(context, "error server not responding " +
e.getMessage(), Toast.LENGTH_LONG).show();
Log.e(LOG_TAG, "Connection Error aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
e);
// Do something else, if wanted.
}
return xmlRespone;
}
Create a global variable like:
Context mContext;
Then add a constructor to your class, in which you accept a Context parameter and assign it to mContext like:
public AgAppHelperMethods(Context context) {
mContext = context;
}
Create an object in your Activity like:
AgAppHelperMethods helper = new AgAppHelperMethods(getBaseContext());
Finally, to show your Toast use:
Toast.makeText(mContext, "error server not responding " + e.getMessage(), Toast.LENGTH_LONG).show();
You need to target your applicationContext in the toast,i can't see you doing that anywhere? And you have outcommented the first line of your toast message?
Edit: Also really really bad codestyle to catch exception e. You should try to narrow down which kind of exception it is that you want to catch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With