Android:无法登录到web会话并在同一登录会话上使用DOM解析XML数据
Android开发:我需要使用POST登录到一个web会话,然后提出另一个请求,我解析XML以获得Lat/Long,然后将其提交给google地图覆盖 我的问题是我能够登录到我的系统,但是当我提交一个解析XML的命令时,它就像一个全新的会话,我基本上从服务器得到了一个“不正确的登录”回复 是否有人有一些简单的步骤来执行此操作…并保持会话为我需要的任意多个命令打开?我不确定我是否在使用会话cookie,但我相信事实就是如此 一些示例代码可能是:Android:无法登录到web会话并在同一登录会话上使用DOM解析XML数据,android,Android,Android开发:我需要使用POST登录到一个web会话,然后提出另一个请求,我解析XML以获得Lat/Long,然后将其提交给google地图覆盖 我的问题是我能够登录到我的系统,但是当我提交一个解析XML的命令时,它就像一个全新的会话,我基本上从服务器得到了一个“不正确的登录”回复 是否有人有一些简单的步骤来执行此操作…并保持会话为我需要的任意多个命令打开?我不确定我是否在使用会话cookie,但我相信事实就是如此 一些示例代码可能是: try { URI loginUri = new
try {
URI loginUri = new URI("http://www.mywebsite.com/ExternalLogin.jsp?user=lee&pwd=bluedog");
URI xmlUri = new URI("http://www.mywebsite.com/getXMLInfo.xml");
// Prepares the request.
HttpClient httpClient = new DefaultHttpClient();
HttpGet loginHttpGet = new HttpGet();
loginHttpGet.setURI(loginUri);
HttpGet xmlHttpGet = new HttpGet();
xmlHttpGet.setURI(xmlUri);
// Sends the request and read the response
HttpResponse loginResponse = httpClient.execute(loginHttpGet);
InputStream loginInputStream = loginResponse.getEntity().getContent();
HttpResponse xmlResponse = httpClient.execute(xmlHttpGet);
InputStream xmlInputStream = xmlResponse.getEntity().getContent();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(xmlInputStream.toString()));
doc.getDocumentElement();
// Continue using DOM to parse my XML data
}
试试{
URI loginUri=新URI(“http://www.mywebsite.com/ExternalLogin.jsp?user=lee&pwd=bluedog");
URI xmlUri=新URI(“http://www.mywebsite.com/getXMLInfo.xml”
//准备请求。
HttpClient HttpClient=新的DefaultHttpClient();
HttpGet loginHttpGet=新的HttpGet();
loginHttpGet.setURI(loginUri);
HttpGet xmlHttpGet=新的HttpGet();
setURI(xmlUri);
//发送请求并读取响应
HttpResponse loginResponse=httpClient.execute(loginHttpGet);
InputStream loginInputStream=loginResponse.getEntity().getContent();
HttpResponse xmlResponse=httpClient.execute(xmlHttpGet);
InputStream xmlInputStream=xmlResponse.getEntity().getContent();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新的InputSource(xmlInputStream.toString());
doc.getDocumentElement();
//继续使用DOM解析我的XML数据
}
您没有说明如何维护我们的“web会话”。我猜这是通过会话cookie实现的。如果是这样,请使用HttpClient并对两个请求继续使用相同的HttpClient
对象。会话cookies将自动处理。谢谢您的回复。我实际上不确定正在使用什么,但我相信它与会话cookie有关。因此,理想情况下,我希望发出一个登录post请求,等待页面开始加载,然后立即使用类似的HttpClient请求发出xml请求,但我还不确定如何做到这一点。一些示例代码可能是这样的:(参见上面的示例代码…我知道它还不正确。任何建议都会非常有用)@Lee:除非你发明了时间机器,否则这是行不通的,当你要求Web服务器开始做其他事情时,它可能还没有处理你的登录请求。我想有时候时间机器更容易发明!我正在研究如何放入等待、等待onload或等待onfinished加载。即使放入Thread.sleep(15000)命令,它也不起作用。使用我上面的代码,cookies是否得到正确处理?它仍然表现得像是一个不同的会议。我完全被难住了。