2012-06-23 51 views
0

我試着去獲取上的AsyncTask一個XML文件,並顯示在ListActivity結果。的AsyncTask永遠不會結束

的問題是,與調試,當我得到的線的 「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]); 
     } 
    } 
} 

回答

1

您可以嘗試把在getmyPStXML()如下:

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

代替

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

,看看你的服務器返回,因爲根據您的堆棧跟蹤有沒有足夠的數據從服務器讀取。

而且它有助於建立連接/數據接收超時,否則您的應用程序將等待來自服務器的一個非常,非常長的時間回覆,並且可能會出現像停止或掛起。

相關問題