0

我试图在 AsyncTask 上获取 xml 文件并在 ListActivity 上显示结果。

问题是,通过调试,当我到达“doc = db.parse(xml);”行时 系统挂起,永远不会回来。

在 Activity Thread 上做了一个“暂停”,并显示了以下堆栈跟踪

堆栈跟踪

OSNetworkSystem.read(FileDescriptor, byte[], int, int) line: not available [native method]  
BlockGuard$WrappedNetworkSystem.read(FileDescriptor, byte[], int, int) line: 273    
PlainSocketImpl.read(byte[], int, int) line: 458    
SocketInputStream.read(byte[], int, int) line: 85   
SocketInputBuffer(AbstractSessionInputBuffer).fillBuffer() line: 103    
SocketInputBuffer(AbstractSessionInputBuffer).read() line: 120  
ChunkedInputStream.getChunkSize() line: 211 
ChunkedInputStream.nextChunk() line: 183    
ChunkedInputStream.read(byte[], int, int) line: 155 
EofSensorInputStream.read(byte[], int, int) line: 159   
InputStreamReader.read(char[], int, int) line: 255  
KXmlParser.peek(int) line: 925  
KXmlParser.pushText(int, boolean, boolean) line: 875    
KXmlParser.nextImpl() line: 354 
KXmlParser.nextToken() line: 1399   
DocumentBuilderImpl.parse(XmlPullParser, DocumentImpl, Node, int) line: 359 

我的 android 模拟器正在使用代理运行,并且工作正常(在浏览器上测试)。尝试了一个很小的xml,但也不起作用...

以上,我的文件。

我的基本.java

public class Basic {

    public InputStream getmyPStXML(String url)
    {
        InputStream xml = null;

        String dstUrl = "http://mypst.com.br" + url;

        try
        {
            // defaultHttpClient
            HttpParams params = new BasicHttpParams();
            params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

            DefaultHttpClient httpClient = new DefaultHttpClient();         
            HttpGet request = new HttpGet(dstUrl);
            request.setHeader("User-Agent", "set your desired User-Agent");

            HttpResponse httpResponse = httpClient.execute(request);

            xml = httpResponse.getEntity().getContent();

        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        catch (ClientProtocolException e)
        {
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;     
    }  

    public Document getDomElement(InputStream xml)
    {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try
        {
            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(xml); 

        }
        catch (ParserConfigurationException e)
        {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        catch (SAXException e)
        {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        catch (IOException e)
        {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        return doc;
    }
}

我的 GetUserGames.java

public class GetUserGames extends ListActivity {

    static final String KEY_JOGOS   = "jogos";
    static final String KEY_JOGO    = "jogo";
    static final String KEY_PIC     = "pic";
    static final String KEY_PIC_BIG = "pic_big";
    static final String KEY_NAME    = "name";

    private ProgressDialog m_ProgressDialog = null; 


    public class getUserGamesASYNC extends AsyncTask<String, Void, Document>
    {

        public Document getUserGames(String username)
        {
            Basic b = new Basic();
            InputStream xml = b.getmyPStXML("/rank/" + username + "/xml/");
            return b.getDomElement(xml);
        }

        protected Document doInBackground(String... username) {
            return getUserGames(username[0]);   
        }
    }
}
4

1 回答 1

1

您可以尝试将以下内容放入getmyPStXML()

Log.i(TAG, EntityUtils.toString( httpResponse.getEntity(), "UTF-8" ) );

代替

xml = httpResponse.getEntity().getContent();

并查看您的服务器返回的内容,因为根据您的堆栈跟踪,没有足够的数据可以从服务器读取。

它还有助于设置连接/数据接收超时,因为否则您的应用程序将等待服务器的回复很长时间,并且可能看起来像停止或挂起。

于 2012-06-24T07:37:16.450 回答