I am receiving a response from a XML request and I need to deal with it. The response will return either '00' when the request has been accepted and the account is verified and a '03' when the account is invalid.
Currently the Sax Reader is returning the correct information but I cannot extract the information from the reader so I can store the username / password into the internal storage of the phone.
The code from the Sax Reader is:
public void inputStreamToString(InputStream is) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean errorCode = false;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
System.out.println("Start Element :" + qName);
if (qName.equalsIgnoreCase("ErrorCode"))
{
errorCode = true;
}
}
public void endElement(String uri, String localName,String qName) throws SAXException
{
System.out.println("End Element :" + qName);
}
public void characters(char ch[], int start, int length) throws SAXException
{
if (errorCode)
{
System.out.println("ErrorCode : "+ new String(ch, start, length));
if (ch.equals(00))
{
System.out.println("IT WORKS!!!");
}
errorCode = false;
}
}
};
saxParser.parse(is, handler);
}
catch (Exception e)
{
System.out.println("Sax Error");
e.printStackTrace();
}
}
As you can see in the public void characters it is printing out the errorcode that is relevant from the response. I am currently trying to do ch.equals(00)
in a If statement but it isn't picking out the correct information! Could it be a problem that ch is a char data type?
Any help will be appreciated.开发者_运维知识库
I got it working. Instead of using '00' and '03' error codes. I have used the length of the response messages which are 'OK' and 'Invalid'.
精彩评论