我试图在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,但也不用...
上面,我的文件。
my 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]);
}
}
}
答案 0 :(得分:1)
您可以尝试将以下内容放入getmyPStXML()
:
Log.i(TAG, EntityUtils.toString( httpResponse.getEntity(), "UTF-8" ) );
而不是
xml = httpResponse.getEntity().getContent();
并查看服务器返回的内容,因为根据您的堆栈跟踪,没有足够的数据可以从服务器读取。
此外,设置连接/数据接收超时也很有帮助,因为否则您的应用程序将等待服务器的回复非常长时间,并且可能看起来像停止或挂起。