0

AsyncTaskでxmlファイルをフェッチし、結果をListActivityに表示しようとしています。

問題は、デバッグで、「doc = db.parse(xml);」という行に到達したときです。システムがハングし、復帰することはありません。

アクティビティスレッドで「一時停止」を実行すると、次のスタックトレースが表示されます

スタックトレース

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を試しましたが、動作しません...

上記、私のファイル。

私のbasic.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 に答える