How to access authenticated URL in Android?
I want to parse the data from RSS Feed that is password protected. If I try to parse the data, it's throwing "File not found Exception"
Here is my code:
try {
DBF = DocumentBuilderFactory.newInstance();
DB = DBF.newDocumentBuilder();
url_val = new URL("http://admin:admin@dev.quaddeals.com/university-of-illinois/androids/city.rss");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
dom = DB.parse(url_val.openConnection().getInputStream());
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
elt = dom.getDocumentElement();
NodeList items = elt.getElementsByTagName("item");
for (int i = 0; i < items.getLength(); i++) {
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for (int j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if (name.equalsIgnoreCase("title")) {
cityTitle = property.getFirstChild().getNodeValue();
}
if (name.equalsIgnoreCase("id")) {
cityId = property.getFirstChild().getNodeValue();
}
}
title.add(cityTitle);
id.add(cityId);
}
VALUE1 = new String[title.size()];
Iterator<String> itc = title.iterator();
while (itc.hasNext()) {
l1++;
VALUE1[l1] = itc.next().toString();
}
VALUE2 = new String[id.size()];
Iterator<String> it1c = id.iterator();
while (it1c.hasNext()) {
m1++;
VALUE2[m1] = it1c.next().toString();
}
} catch (Exception e) {
}
My Log Cat shows error as follows:
02-21 23:20:48.893: WARN/System.err(19206): java.io.FileNotFoundException: http://dev.quaddeals.com/university-of-illinois/androids/city.rss
02-21 23:20:48.893: WARN/System.err(19206): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection开发者_JS百科.getInputStream(HttpURLConnection.java:1064)
02-21 23:20:48.893: WARN/System.err(19206): at com.fsp.quaddeals.DealCities$SelectDataTask_Deals.doInBackground(DealCities.java:341)
02-21 23:20:48.893: WARN/System.err(19206): at com.fsp.quaddeals.DealCities$SelectDataTask_Deals.doInBackground(DealCities.java:1)
If I enter this URL in browser it will ask for the authentication, after that it will redirected to the original RSS FEED where my parsing data exists.
I want to parse the data in the this page. How can I access this password protected page and parse the data? Whether Android supports SSL connections.
You have a few options here. You can set it manually, like this:
URL url = new URL("http://admin:admin@dev.quaddeals.com/university-of-illinois/androids/city.rss");
URLConnection urlConnection = url.openConnection();
if (url.getUserInfo() != null) {
String basicAuth = "Basic " + new String(new Base64().encode(url.getUserInfo().getBytes()));
urlConnection.setRequestProperty("Authorization", basicAuth);
}
dom = DB.parse(urlConnection.getInputStream());
You can use java.net.Authenticator
, but it works globally (on all URLs):
// see https://developer.android.com/reference/java/net/Authenticator.html
// and https://developer.android.com/reference/java/net/PasswordAuthentication.html
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("admin", "admin".toCharArray());
}
});
You can use a library like Volley (example), or my personal preference, okhttp (example).
精彩评论