Question

I'm making an android app. It has an XML parser. The XML is type of my api's response This is The Way I downloaded it to InputStream :

private InputStream downloadUrl(String urlString) throws IOException {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            String encoded;
            encoded = "Basic " + Base64.encodeToString("user:pass".getBytes(), 0);
            conn.setRequestProperty("Authorization", encoded);
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            InputStream stream = conn.getInputStream();
            return stream;
        }

The XML's output if the user and pass are correct is :

<status>true</status>

if it's not :

<status>false</status>

and parser class is :

private static final String ns = null;

    public Boolean parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser);
        } finally {
            in.close();
        }
    }
    private Boolean readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {

        parser.require(XmlPullParser.START_TAG, ns, "verify_account");
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            if (name.equals("status")) {
                return readSta(parser);
            } else {
                skip(parser);
            }
        }
        return false;
    }
    private Boolean readSta(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "stats");
        String name = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "stats");
        if (name == "true"){
            return true;
        }
        else {
            return false;
        }
    }
    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }

the problem is that the class PareXml always returns false :| but I printed the tag's value and it was "true" i tried adding spaces before and after the "true" in readSta function line 4 but it's not working :| . it seems to be an encoding problem

I tried many things but it's still not working :|

Was it helpful?

Solution

In your readSta method, instead of using :

if (name == "true"){
            return true;
        }

use:

if (name.equals("true")){
            return true;
        }

Try this once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top