Skip to content Skip to sidebar Skip to footer

Getting Xml From Web Behind Authentication

me again, sorry :) I was using dom parser to get xml from web and parse it and put the data in db.. all was fine and dandy but than I put basic authentication for folder where the

Solution 1:

I have done it ! took about 4 hours to learn it and figure it out.. and lots and lots of googling :D

but here is how I did it, maybe somebody can use it also:

URIlUri=newURI(getString(R.string.url)); //get url from strings// XML node keysfinalStringKEY_ITEM="plan"; // parent nodefinalStringKEY_NAME="agent";
finalStringKEY_DATE="date";
finalStringKEY_SHIFT="shift";
finalStringKEY_LINE="line";

XMLhandlerparser=newXMLhandler();

// Prepares the requestHttpClientlHttpClient=newDefaultHttpClient();
HttpGetlHttpGet=newHttpGet();
lHttpGet.setURI(lUri);
lHttpGet.addHeader(BasicScheme.authenticate(newUsernamePasswordCredentials("user", "pass"), "UTF-8", false));

// Sends the request and read the responseHttpResponselHttpResponse= lHttpClient.execute(lHttpGet);
InputStreamlInputStream= lHttpResponse.getEntity().getContent();

DocumentBuilderFactoryfactory= DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder= factory.newDocumentBuilder();
Documentdom= builder.parse(lInputStream);

Elementroot= dom.getDocumentElement();
NodeListnl= root.getElementsByTagName(KEY_ITEM);

// pass data to another function...for (inti=0; i < nl.getLength(); i++) {
   Elemente= (Element) nl.item(i);
   Stringname= parser.getValue(e, KEY_NAME);
   Stringdate= parser.getValue(e, KEY_DATE);
   Stringshift= parser.getValue(e, KEY_SHIFT);
   Stringline= parser.getValue(e, KEY_LINE);
   db.createList(name, date, shift, line);
}

Working so far, its not the best solution I guess but hey! its working :)

Post a Comment for "Getting Xml From Web Behind Authentication"