【问题标题】:getting XML from web behind authentication在身份验证后从 Web 获取 XML
【发布时间】:2012-01-27 17:50:27
【问题描述】:

又是我,对不起:)

我正在使用 dom 解析器从 web 获取 xml 并对其进行解析并将数据放入 db 中。一切都很好而且花花公子,但我对 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 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

}

如何对此实施身份验证?我知道我应该使用类似的东西:

Authenticator.setDefault(new Authenticator() {
 @Override
        protected PasswordAuthentication getPasswordAuthentication() {
         return new PasswordAuthentication(
   "user", "password".toCharArray());
        }
});

从我在这个论坛和谷歌上看到的......但到目前为止没有运气,我不明白java这么好(还在学习)我该怎么做?我的意思是如何在对解析器进行身份验证后获取 xml,我最终得到了流,而解析器想要字符串......

我知道我可能没有多大意义:)

弗拉德。

【问题讨论】:

    标签: java android xml http parsing


    【解决方案1】:

    我做到了!花了大约 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);
    }
    

    到目前为止工作,我猜它不是最好的解决方案,但是嘿!它的工作:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 2020-11-04
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多