I am fetching the some data from the server using XML parsing that is not working with ICS version of Android.here is my please tell me what correction do I make so that I should also run on ICS...(It's working fine with lower versions). Here is my code
try {
URL url = new URL(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("file");
namephoto = new String[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("file");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
namephoto[i] = ((Node) nameList.item(0)).getNodeValue();
}
} catch (Exception e) {
Log.e("name", "" + e);
}
photobitmap = new Bitmap[namephoto.length];
setPhotoBackground(namephoto[index_photo]);
My XML code like this.
<?xml version="1.0"?>
-<root><file>1 a.JPG</file><file>2 b.JPG</file><file>3 c.JPG</file><file>4 d.JPG</file> </root>
i have got the solution myself.. Here is the code which is compatible with Android 4.0 as well as rest of the android versions...Just change the for loop like this.
for (int i = 0; i < nodeList.getLength(); i++) {
Node name = nodeList.item(i);
NodeList nodeEle = name.getChildNodes();
namephoto[i] = ((Node) nodeEle.item(0)).getNodeValue();
}
Related
I use this method for get rss items
public static ArrayList<RssItem> getRssItems(String feedUrl) {
ArrayList<RssItem> rssItems = new ArrayList<RssItem>();
try {
URL url = new URL(feedUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(is);
Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("item");
if (nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
Element entry = (Element) nodeList.item(i);
Element _titleE = (Element) entry.getElementsByTagName(
"title").item(0);
Element _descriptionE = (Element) entry
.getElementsByTagName("description").item(0);
Element _pubDateE = (Element) entry
.getElementsByTagName("pubDate").item(0);
Element _linkE = (Element) entry.getElementsByTagName(
"link").item(0);
String _title = _titleE.getFirstChild().getNodeValue();
String _description = _descriptionE.getFirstChild()
.getNodeValue();
Date _pubDate = new Date(_pubDateE.getFirstChild()
.getNodeValue());
String _link = _linkE.getFirstChild().getNodeValue();
RssItem rssItem = new RssItem(_title, _description,
_pubDate, _link);
rssItems.add(rssItem);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return rssItems;
}
but when I set uses-sdk in manifest, after this string
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK)
execution proceeds to block catch. When I delete uses-sdk from manifest everything is OK. What I need to do to leave uses-sdk but it work?
The question doesn't really describe the problem very well, but making an educated guess, you are getting NetworkOnMainThreadException.
When you don't specify a targetSdkVersion in manifest, it defaults to 1 and all backwards-compatibility features are enabled, including allowing network operations on UI thread. When you specify a target SDK version >= 11 and actually run on API level 11 or higher, you'll get NetworkOnMainThreadException.
The fix is to do network operations on a background thread using e.g. AsyncTask.
Canonical reference: How to fix android.os.NetworkOnMainThreadException?
I want to read a XML document from a URL:
public void DownloadXmlFile() throws IOException{
//TODO
String url = "http://api.m1858.com/coursebook.xml";
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
}
I get an Error Exception
android.os.NetworkOnMainThreadException
I added uses-permission in manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This is not an XML Problem its a Strict Mode Problem.
You should'nt do time intensiv things in Gui Thread, do it in a own Thread.
Blogpost with introduction
Developers API infos
BestPracties
However, you can disable it, but you shouldt ;)
see here
there are two step for read data from server...
1.Make a HTTP request to get the data from the webservice
2.Parse a XML document and read the contents
try
{
URL url = new URL("http://www.w3schools.com/xml/note.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("note");
/** Assign textview array lenght by arraylist size */
to = new TextView[nodeList.getLength()];
from = new TextView[nodeList.getLength()];
heading = new TextView[nodeList.getLength()];
body = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++)
{
Node node = nodeList.item(i);
to[i] = new TextView(this);
from[i] = new TextView(this);
body[i] = new TextView(this);
heading[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList toList = fstElmnt.getElementsByTagName("to");
Element nameElement = (Element) toList.item(0);
toList = nameElement.getChildNodes();
to[i].setText("To = "+ ((Node) toList.item(0)).getNodeValue());
NodeList fromList = fstElmnt.getElementsByTagName("from");
Element fromElement = (Element) fromList.item(0);
fromList = fromElement.getChildNodes();
from[i].setText("from = "+ ((Node) fromList.item(0)).getNodeValue());
NodeList headingList = fstElmnt.getElementsByTagName("heading");
Element headingElement = (Element) headingList.item(0);
headingList = headingElement.getChildNodes();
heading[i].setText("heading = "+ ((Node) headingList.item(0)).getNodeValue());
NodeList bodyList = fstElmnt.getElementsByTagName("body");
Element bodyElement = (Element) bodyList.item(0);
bodyList = bodyElement.getChildNodes();
body[i].setText("body = "+ ((Node) bodyList.item(0)).getNodeValue());
layout.addView(to[i]);
layout.addView(from[i]);
layout.addView(heading[i]);
layout.addView(body[i]);
}
}
catch (Exception e)
{
System.out.println("XML Pasing Excpetion = " + e);
}
Why you don't google or look for the error here on stackoverflow? It's full of answers...
You have to extend an AsyncTask to avoid the blocking of the GUI and do this kind of operation (as downloading or parsing stuff) in background.
I am fetching the some data from the server using XML parsing that is not working with ICS version of Android.here is my please tell me what correction do I make so that I should also run on ICS...(It's working fine with lower versions). Here is my code
try {
URL url = new URL(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("file");
namephoto = new String[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("file");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
namephoto[i] = ((Node) nameList.item(0)).getNodeValue();
}
} catch (Exception e) {
Log.e("name", "" + e);
}
photobitmap = new Bitmap[namephoto.length];
setPhotoBackground(namephoto[index_photo]);
My XML code like this.
<?xml version="1.0"?>
-<root><file>1 a.JPG</file><file>2 b.JPG</file><file>3 c.JPG</file><file>4 d.JPG</file> </root>
i have got the solution myself.. Here is the code which is compatible with Android 4.0 as well as rest of the android versions...Just change the for loop like this.
for (int i = 0; i < nodeList.getLength(); i++) {
Node name = nodeList.item(i);
NodeList nodeEle = name.getChildNodes();
namephoto[i] = ((Node) nodeEle.item(0)).getNodeValue();
}
I'm able to get the information I want if the XML file is stored locally on my machine, but reading it when stored on the phone isn't working very well.
I've tried XMLPullParser but it extracts binary information about the id names etc and I'd like the actual name.
Code:
final String ANDROID_ID = "android:id";
try {
File fXmlFile = new File("res/layout/page1.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Button");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
if (eElement.hasAttribute(ANDROID_ID))
System.out.println("ID: "
+ eElement.getAttribute(ANDROID_ID));
}
}
}
catch (Exception e) {
System.out.println("Catch");
e.printStackTrace();
}
in the XmlPullParser documentation there is a getAttributeCount() and getAttributeByName(int index) that might be useful. You must use it in START_TAG
Over XML parsing this link describes well. Here you can find other parsers too with xmlPullParser.
i using the follwing Code to retrive XML element text using getElementsByTagName
this code success in 2.2 and Failed in 2.1
any idea ?
URL metafeedUrl = new URL("http://x..../Y.xml")
URLConnection connection ;
connection= metafeedUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection ;
int resposnseCode= httpConnection.getResponseCode() ;
if (resposnseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf ;
dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Parse the Earthquakes entry
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
//ArrayList<Album> Albums = new ArrayList<Album>();
/* Returns a NodeList of all descendant Elements with a given tag name, in document order.*/
NodeList nl = docEle.getElementsByTagName("entry");
if (nl!=null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
Element entry = (Element)nl.item(i);
/* Now on every property in Entry **/
Element title =(Element)entry.getElementsByTagName("title").item(0);
*Here i Get an Error*
String album_Title = title.getTextContent();
Element id =(Element)entry.getElementsByTagName("id").item(0);
String album_id = id.getTextContent(); //
getTextContent() is not supported in API 7 (Android 2.1). It was introduced in API 8 (2.2).
Assuming a predictable result from the server, you can use the following code:
Node n = aNodeList.item(i);
String strValue = n.getFirstChild().getNodeValue();
// as opposed to the String strValue = n.getTextContent();
If the element may be empty, then you'd want to check the child count first.