-1

我有一个不扩展活动的类,如果没有可用的 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)
4

2 回答 2

0

您可以使用此代码段检查互联网是否可用,

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager
                .getActiveNetworkInfo();

        if (activeNetworkInfo == null) {

            Toast.makeText(Photos_Activity.this,
                    "Network Not Connected...Please Try Again",
                    Toast.LENGTH_LONG).show();

        } else {
            if (activeNetworkInfo.isConnected()) {

                //Here you can perform your task when internet is connected 

            } else if (activeNetworkInfo.isConnectedOrConnecting()) {

                Toast.makeText(Photos_Activity.this,
                        "Network is connecting Now please patient",
                        Toast.LENGTH_LONG).show();
            }

        }

不要忘记在清单中添加此权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
于 2012-09-14T11:49:05.860 回答
0

您必须先检查网络可用性,然后再进行进一步处理

此功能可能会帮助您完成并添加所需的权限

public static boolean isNetworkAvailable(Context context) {
        boolean outcome = false;

        if (context != null) {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
            for (NetworkInfo tempNetworkInfo : networkInfos) {

                if (tempNetworkInfo.isConnected()) {
                    outcome = true;
                    break;
                }
            }
        }

        return outcome;
    }
于 2012-09-14T11:50:56.427 回答