经过长时间的努力,我在这段代码中诊断出我的问题是,如果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);
答案 0 :(得分:0)
每次进入AgAppXMLParser
时都会将其初始化为true,因为该变量为静态:
public static String[][] AgAppXMLParser( String parUrl) {
String _node,_element;
String[][] xmlRespone = null;
HttpURLConnection urlConnection = null;
flag = true;
.....