Java 通过身份验证从web获取XML
又是我,对不起:) 我使用dom解析器从web获取xml并对其进行解析,然后将数据放入数据库中。。一切都很好,但比我把xml所在的文件夹的基本身份验证放在web上 在它像这样工作之前:Java 通过身份验证从web获取XML,java,android,xml,http,parsing,Java,Android,Xml,Http,Parsing,又是我,对不起:) 我使用dom解析器从web获取xml并对其进行解析,然后将数据放入数据库中。。一切都很好,但比我把xml所在的文件夹的基本身份验证放在web上 在它像这样工作之前: final String URL = getString(R.string.url); // XML node keys final String KEY_ITEM = "plan"; // parent node final String KEY_NAME = "agent"; final String KE
final String URL = getString(R.string.url);
// XML node keys
final String KEY_ITEM = "plan"; // parent node
final String KEY_NAME = "agent";
final String KEY_DATE = "date";
final String KEY_SHIFT = "shift";
final String KEY_LINE = "line";
XMLhandler parser = new XMLhandler();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// empty table
db.dropData("plan");
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
String name = parser.getValue(e, KEY_NAME);
String date = parser.getValue(e, KEY_DATE);
String shift = parser.getValue(e, KEY_SHIFT);
String line = parser.getValue(e, KEY_LINE);
db.createList(name, date, shift, line); // add to db
}
从我在这个论坛上看到的,谷歌。。。但是到目前为止没有运气,我不懂java那么好(还在学习)我该怎么做呢?我的意思是,在对解析器进行身份验证之后,如何获取xml,我最终得到的是流,而解析器需要字符串
我知道我可能没什么道理:)
弗拉德。我已经做到了!花了大约4个小时来学习和理解。。还有很多很多的谷歌搜索:D 但我是这样做的,也许有人也可以使用它:
URI lUri = new URI(getString(R.string.url)); //get url from strings
// XML node keys
final String KEY_ITEM = "plan"; // parent node
final String KEY_NAME = "agent";
final String KEY_DATE = "date";
final String KEY_SHIFT = "shift";
final String KEY_LINE = "line";
XMLhandler parser = new XMLhandler();
// Prepares the request
HttpClient lHttpClient = new DefaultHttpClient();
HttpGet lHttpGet = new HttpGet();
lHttpGet.setURI(lUri);
lHttpGet.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("user", "pass"), "UTF-8", false));
// Sends the request and read the response
HttpResponse lHttpResponse = lHttpClient.execute(lHttpGet);
InputStream lInputStream = lHttpResponse.getEntity().getContent();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(lInputStream);
Element root = dom.getDocumentElement();
NodeList nl = root.getElementsByTagName(KEY_ITEM);
// pass data to another function...
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
String name = parser.getValue(e, KEY_NAME);
String date = parser.getValue(e, KEY_DATE);
String shift = parser.getValue(e, KEY_SHIFT);
String line = parser.getValue(e, KEY_LINE);
db.createList(name, date, shift, line);
}
uriluri=newuri(getString(R.string.url))//从字符串获取url
//XML节点密钥
最终字符串键\u ITEM=“计划”//父节点
最终字符串KEY_NAME=“agent”;
最终字符串键\u DATE=“DATE”;
最终字符串键\u SHIFT=“SHIFT”;
最终字符串键\u LINE=“LINE”;
XMLhandler解析器=新的XMLhandler();
//准备请求
HttpClient lHttpClient=新的DefaultHttpClient();
HttpGet lHttpGet=新的HttpGet();
lHttpGet.setURI(lUri);
lHttpGet.addHeader(基本方案验证(新用户名密码凭据(“用户”、“通过”),“UTF-8”,false));
//发送请求并读取响应
HttpResponse lHttpResponse=lHttpClient.execute(lHttpGet);
InputStream lInputStream=lHttpResponse.getEntity().getContent();
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
文档dom=builder.parse(lInputStream);
Element root=dom.getDocumentElement();
NodeList nl=root.getElementsByTagName(键项);
//将数据传递给另一个函数。。。
对于(int i=0;i
工作到目前为止,这不是最好的解决方案,我想,但嘿!它的工作:)我已经完成了!花了大约4个小时来学习和理解。。还有很多很多的谷歌搜索:D 但我是这样做的,也许有人也可以使用它:
URI lUri = new URI(getString(R.string.url)); //get url from strings
// XML node keys
final String KEY_ITEM = "plan"; // parent node
final String KEY_NAME = "agent";
final String KEY_DATE = "date";
final String KEY_SHIFT = "shift";
final String KEY_LINE = "line";
XMLhandler parser = new XMLhandler();
// Prepares the request
HttpClient lHttpClient = new DefaultHttpClient();
HttpGet lHttpGet = new HttpGet();
lHttpGet.setURI(lUri);
lHttpGet.addHeader(BasicScheme.authenticate(new UsernamePasswordCredentials("user", "pass"), "UTF-8", false));
// Sends the request and read the response
HttpResponse lHttpResponse = lHttpClient.execute(lHttpGet);
InputStream lInputStream = lHttpResponse.getEntity().getContent();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(lInputStream);
Element root = dom.getDocumentElement();
NodeList nl = root.getElementsByTagName(KEY_ITEM);
// pass data to another function...
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
String name = parser.getValue(e, KEY_NAME);
String date = parser.getValue(e, KEY_DATE);
String shift = parser.getValue(e, KEY_SHIFT);
String line = parser.getValue(e, KEY_LINE);
db.createList(name, date, shift, line);
}
uriluri=newuri(getString(R.string.url))//从字符串获取url
//XML节点密钥
最终字符串键\u ITEM=“计划”//父节点
最终字符串KEY_NAME=“agent”;
最终字符串键\u DATE=“DATE”;
最终字符串键\u SHIFT=“SHIFT”;
最终字符串键\u LINE=“LINE”;
XMLhandler解析器=新的XMLhandler();
//准备请求
HttpClient lHttpClient=新的DefaultHttpClient();
HttpGet lHttpGet=新的HttpGet();
lHttpGet.setURI(lUri);
lHttpGet.addHeader(基本方案验证(新用户名密码凭据(“用户”、“通过”),“UTF-8”,false));
//发送请求并读取响应
HttpResponse lHttpResponse=lHttpClient.execute(lHttpGet);
InputStream lInputStream=lHttpResponse.getEntity().getContent();
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
文档dom=builder.parse(lInputStream);
Element root=dom.getDocumentElement();
NodeList nl=root.getElementsByTagName(键项);
//将数据传递给另一个函数。。。
对于(int i=0;i
工作到目前为止,这不是最好的解决方案,我想,但嘿!其工作原理:)