0

经过长时间的努力,我在这段代码中诊断出我的问题是,如果块中的flag错误catch永远不会成为真的..即使我提供有效的输入
,请告诉我它是怎么回事?当标志值在 catch 块中变为 false 时如何解决该问题在 1 次为 false 后始终显示为 false 并在我提供正确输入时打印无效输入消息

        public class AgAppHelperMethods   {

     private static final String LOG_TAG = null;

   private static AgAppHelperMethods instance = null;
      static boolean flag=true;

   public static String varMobileNo;
    public static String varPinNo;

   String[][] xmlRespone = null;





    public static AgAppHelperMethods getInstance() 
   {
       if(instance == null) 
          {
              instance = new AgAppHelperMethods();
          }
      return instance;
   }





public static   String[][] AgAppXMLParser( String parUrl) {


    String _node,_element;
     String[][] xmlRespone = null;
     HttpURLConnection urlConnection = null;


     try {



     String url = ("www.xyz.com....");
     URL finalUrl = new URL(url);   

      urlConnection = (HttpURLConnection) finalUrl.openConnection();

      urlConnection.setUseCaches(false); 




          urlConnection.connect();



    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new    
             InputSource(urlConnection.getInputStream()));
            doc.getDocumentElement().normalize();

            NodeList list=doc.getElementsByTagName("*");
            _node=new String();
            _element = new String();
            xmlRespone = new String[list.getLength()][2];

            //this "for" loop is used to parse through the
            //XML document and extract all elements and their
            //value, so they can be displayed on the device

            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

            urlConnection.disconnect();



        }//end try











    catch (Exception e)
    {
        String[][] res;
     flag=false;

   Log.e(LOG_TAG, "CONNECTION ERROR  FUNDAMO SERVER NOT RESPONDING", e);













     public class AgAppTransAirTimeTopUp  extends Activity {





                btnTATsubmit.setOnClickListener(new OnClickListener()
        {
        public void onClick(View v)

        {


                                submitATTP();
             }

        }

                                        }); 



                protected void submitATTP() {

                       xmlResponse = AgAppHelperMethods.AgAppXMLParser("Axyz......");
                 if(!AgAppHelperMethods.flag)
                    {

    Toast.makeText(getApplicationContext(), "Invalid Input " ,     
             Toast.LENGTH_SHORT).show();

            }
    else {

 Intent j = new Intent(AgAppTransAirTimeTopUp.this, AgAppTransATTPResponse.class);
4

1 回答 1

0

每次输入时都用 true 初始化它AgAppXMLParser,因为变量是静态的:

public static   String[][] AgAppXMLParser( String parUrl) { 
   String _node,_element;
   String[][] xmlRespone = null;
   HttpURLConnection urlConnection = null;

   flag = true;

   .....
于 2012-10-03T07:45:15.637 回答