嗨,这是我的助手类,我在其中检查内部连接和 xml 解析并将此类用于另一个活动问题是当服务器连接正常但当服务器没有响应或无效输入代码突然停止时,我发现 asyntask 来解决这个问题isse 但我的问题是如何在这段代码中使用 AsyncTask ?或者如果服务器没有响应错误消息连接错误 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 显示在日志上但不显示在 toast 上,我该怎么做才能使我的应用程序在服务器没有响应时不会爆炸?任何想法?
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;
}