Pullparser没有得到inputStream

时间:2015-07-05 18:19:35

标签: android xml http xml-parsing

我正在尝试从xml响应中获取数据,数据在那里,我已经将它打印在日志中将其转换为String,但是当我尝试使用xml解析器解析它时,它并没有。工作。

相关代码:

            URL myUrl = new URL(_url);
            URLConnection connection = myUrl.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection)connection;
            httpConnection.setReadTimeout(10000);
            httpConnection.setConnectTimeout(15000);
            //>>
            httpConnection.setRequestMethod("POST");
            httpConnection.setDoOutput(true);
            httpConnection.setDoInput(true);
            httpConnection.connect();
            int responseCode = httpConnection.getResponseCode();
            Log.d("OpenDataStream", "response: " +  responseCode);
            if(responseCode== HttpURLConnection.HTTP_OK)
            {
                InputStream inStream = httpConnection.getInputStream();
                //Log.v("OpenDataStream", readIt(inStream, 500)); << This method shows me that data is getting retrieved
                switch (process)
                {
                    case LOGIN:
                    {
                        ProcessLogin(inStream, responseCode);
                    }
                }
            }
...



public boolean ProcessLogin(InputStream myInStream, int responseCode)
    {
        Player myPlayer = new Player();
        XmlPullParserFactory myXMLFactory;
        try
        {
            myXMLFactory = XmlPullParserFactory.newInstance();
            myXMLFactory.setNamespaceAware(true);
            XmlPullParser pullParser = myXMLFactory.newPullParser();
            pullParser.setInput(myInStream, null);
            int eventType = pullParser.getEventType();
            while(eventType!= XmlPullParser.END_DOCUMENT)
            {
                Log.w("PULLPARSER", "eventType: " + eventType);
                Log.w("PULLPARSER", "pullParser: " +  pullParser.getName());
                if(eventType==XmlPullParser.START_TAG && pullParser.getName().equals("response"))
                {
                    Log.w("PULLPARSER", "Starting doc");
                    eventType= pullParser.next();
                    while(!(eventType==XmlPullParser.END_TAG && pullParser.getName().equals("response")))
                    {
                        Log.w("PULLPARSER", "Starting response");
                        if(eventType==XmlPullParser.START_TAG)
                        {
                            if(pullParser.getName().equals("user_id"))
                            {
                                myPlayer.setIdUser(pullParser.nextText());
                            }                                   
                        }
                        eventType=pullParser.next();
                    }
                }
                eventType=pullParser.next();
            }

            Log.v("PullParser" , "Finished");
            return new SharedPrefController(myContext).savePlayer(myPlayer);
        }
        catch (XmlPullParserException ex)
        {
            Log.w("PULLPARSER","XML PUll Parser Exception",ex);
        }
        catch (IOException ex)
        {
            Log.w("PULLPARSER","IO Exception",ex);
        }

        return false;
    }

我在日志中得到的是:

D/OpenDataStream﹕ response: 200
V/OpenDataStream﹕ <?xml version="1.0" encoding="utf-8"?>
        <response><user_id>6386</user_id><user_email>test@test.com</user_email><user_name>Test</user_name><user_lastname>Tests</user_lastname><user_status>2</user_status></response>    
W/PULLPARSER﹕ pullParser: null
W/PULLPARSER﹕ eventType: 1
V/PullParser﹕ Finished

知道出了什么问题吗?

0 个答案:

没有答案