我有一个不扩展活动的类,如果没有可用的 Internet 连接,则会显示错误
如果 Internet 可用,它工作正常,但当 Internet 不可用或服务器繁忙时,应用程序强制关闭。我该如何防止这种情况?如何在主要活动类中打印捕获块消息?
如果没有可用的网络,如何阻止应用程序崩溃?
public class AgAppHelperMethods {
private static final String LOG_TAG = null;
public static String[][] AgAppXMLParser( String parUrl) {
String _node,_element;
String[][] xmlRespone = null;
try {
String url = "www.xxxx.com";
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)
{
Log.e(LOG_TAG, "Connection Error NET NOT WORKING", e);
}
return xmlRespone;
}
public class LoginScreen extends Activity implements Serializable {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.agapplogin);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
public void
onClick(View view) {
postLoginData();
public void postLoginData()
{
xmlRespone = AgAppHelperMethods.AgAppXMLParser( "www.xxx.com);
Intent i = new Intent(this.getApplicationContext(),
Activity2.class);
Bundle bundle = new Bundle();
bundle.putSerializable("xmlResponee", xmlRespone.toString());
i.putExtras(bundle);
//i.putExtra("OBJECT", xmlRespone)