simple XML DOM parser example????? - Android Software Development

can anyone here know how to make it work with a local xml file? been stuck for weeks.
thanks

DOM or Sax
kadmos said:
can anyone here know how to make it work with a local xml file? been stuck for weeks.
Click to expand...
Click to collapse
I'm new to Android dev but the only examples I've come across of xml processing is using the XmlResourceParser, which uses a Sax-like technique rather than walking an in-memory dom.
The books Pro Android 2 and Beginning Android 2 have decent examples.
Sorry, I tried to post links above but was not allowed to because I'm new here ...

Here's a DOM example ...
rmhand said:
I'm new to Android dev but the only examples I've come across of xml processing is using the XmlResourceParser, which uses a Sax-like technique rather than walking an in-memory dom.
Click to expand...
Click to collapse
Ha! Well, since posting that less than an hour ago I've discovered that you can in fact create a Document Object Model from an XML file. The Beginning Android 2 book has an example that looks something like this:
Code:
InputStream in = getResources().openRawResource(R.raw.myXmlFile);
DocumentBuilder builder = DocumentBuilderFactory
.newInstance().newDocumentBuilder();
Document doc = builder.parse(in, null);
NodeList animals = doc.getElementsByTagName("animal");
for (int i=0;i<animals.getLength();i++) {
items.add(((Element)animals.item(i)).getAttribute("species"));
}
in.close();

IBM has a very good example of all xml parsers available for Android:
http://www.ibm.com/developerworks/opensource/library/x-android/index.html

Related

dynamically setting image resources

greetings
was referred to this forum from someone at androidcommunity.com regarding this...
i searched the forum but couldnt find any relevant posts - can anyone point me in the right direction for doing this properly?
specifically how could one set an image resource based on a string variable being used for part of the image's file name
i tried this based on a post i saw elsewhere:
myContext.getResources().getIdentifier(myStringVariable + "_thumbnail", "drawable", myContext.getPackageName()));
but it returnes a string(or an integer?) of numbers (the resource id?) that setImageResource couldnt use unless i just wasnt doing it properly.
is there perhaps a way to get the resource name based on that id number or whatever it is that im getting?
apreesh
33 views
dang
getIdentifier() returns int and yes, it could be used in setImageResource() method.
But why you want to set resources from strings?
because the image being set is based on user selection and is not just one image its several associated images so there is a number sequence to the image file names as well that i did not show in the snippet i posted.
but i went back and plugged some of those returned values (resources ids?) from getIdentifier() into setImageResource() and it does indeed work so thanks for that - i have an idea what i was doing wrong before but for the sake of moving on im using a different solution now - in short i am now defining each group of images as a separate class member int[] and i will probably use a switch case to plug the correct one into the gridview. its ugly, but i currently only have 11 different groups and no more than 16 images per group so it will work for now until i can study the resource object more and figure out a way to get counts of associated image resources based on a part of the resource name, like with a regular expression or something, because thats the next problem i will have to deal with if i am not pre-defining all of these arrays.
if you know of a way to do that that would be awesome but ill will probably look into it more myself once i get this app closed up and can go back and fix stuff. im pressed for time right now.
thanks
It's really bad thing to use getIdentifier() method, we should always use R class. I think your problem resides somewhere before, you try to do something, that you shouldn't
How do you get these strings? You mentioned they are from user, but he doesn't write them by hand, right? If this is some selectable list, etc., they should be ints, enums, or some objects from the beginning. Not strings. Parsing strings is always ugly.
Ahh and if you have group of many small images, it is usually better to concatenate them into one big image - it's more efficient and you don't have to use 200 R constants in your code.
the string comes from the tag associated with a clickable imageView selected from the previous screen - a menu item if you will. the string will serve several purposes, retrieving related data, etc, but the first thing i needed to work out was retrieving the correct images and displaying them. i dont know how i could concatenate the images into one big image because each one needs to be clickable itself and handle certain events associated with itself.
i will go ahead and admit this is my first app so im basically figuring stuff out as i go. and learning most of my oop from flash has probably handicapped me
i appreciate your help dude
Brut.all said:
it's more efficient and you don't have to use 200 R constants in your code.
Click to expand...
Click to collapse
the only other thing i could think of trying was creating an xml doc to group the associated resource names together and figure out how to read from that to know which images to set
i dont see any methods in the R class i could use for sorting, grouping and then retrieving certain resources based on user interaction
switch cant eval string types...!?
kadmos said:
switch cant eval string types...!?
Click to expand...
Click to collapse
Nope
As I said, strings aren't good for identifying things - regardless of the language used. This is why people created int constants and/or enums.
And no, I doubt there are some mechanisms of grouping resources, etc. It must be simple, you are trying to complicate everything
I have a strong feeling that you should change your app architecture and get rid of strings. But here quick general fix (not a good solution! but just works).
Map your strings to R ints:
Code:
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("button_normal", R.drawable.button_normal);
map.put("button_pressed", R.drawable.button_pressed);
// etc
Accessing will be done:
Code:
map.get("button_" + state); // Return int id, use as you need.
This is a bad practice, but it will work. Consider re-archirecturing your app.
@Brut.all: do you have any plans on updating apktool with 2.2 support?
@kadmos
Full example:
Code:
public static enum Planet {
MERCURY(R.string.planet_mercury, R.drawable.planet_mercury),
VENUS(R.string.planet_venus, R.drawable.planet_venus),
EARTH(R.string.planet_earth, R.drawable.planet_earth),
MARS(R.string.planet_mars, R.drawable.planet_mars);
public final int nameResId;
public final int imageResId;
public static Planet findByNameResId(int nameResId) {
for (Planet p : values()) {
if (p.nameResId == nameResId) {
return p;
}
}
return null;
}
private Planet(int nameResId, int imageResId) {
this.nameResId = nameResId;
this.imageResId = imageResId;
}
}
You have enum of planets, each of them has its name and image. Then you could do something like:
Code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
for (Planet planet : Planet.values()) {
menu.add(0, planet.nameResId, 0, planet.nameResId);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Planet planet = Planet.findByNameResId(item.getItemId());
doSomethingWithPlanetImage(planet.imageResId);
return true;
}
You identifies planets by ints (nameResId in this example - of course it must be unique), not by strings. Operations on ints are several times faster, than on strings, this is why Google decided to identify all things: resources, menu items, etc. just by ints.
Ahh and no, writing switch-cases to do something depending on given object isn't true OOP. OOP is above: enums know, which drawable is connected to them, there is no need for switches.
AuxLV said:
@Brut.all: do you have any plans on updating apktool with 2.2 support?
Click to expand...
Click to collapse
Yeah, of course, I want to work on apktool this weekend. Unfortunately baksmali doesn't support Froyo yet, so I can't support it fully neither.
@Brut.all:
Ha, I recognise that example It's from the Java Trail/tutorial on enums isn't it? Except they used gravity rather than drawable references.
@kadmos:
Are all the images known from the beginning? In other words, is the user creating them at runtime or are you including them with your app? If they are included with your APK then normally, as Brut said, you should be able to use the identifiers directly.
Concatenating the images all into one isn't hard to do, as you can still draw the specific bitmaps out using the Bitmap.create(bitmapToGetAPartOutOf, ....) method. You can then make those individual bitmaps into ImageViews and only have to remember the 'grid reference' for where they came out of the big image. That said, you'd have to balance the added complexity of creating the big images against the ease of not having loads of R constants. I can't really say anymore because I'm not fully following what you're trying to achieve.
Steven__ said:
@Brut.all:
Ha, I recognise that example It's from the Java Trail/tutorial on enums isn't it? Except they used gravity rather than drawable references.
Click to expand...
Click to collapse
I took planets example, because it's good, but everything was written from scratch
Steven__ said:
Concatenating the images (...)
Click to expand...
Click to collapse
I'm pretty sure I saw this concatenating approach somewhere in official Android's guidelines for performance, but now I can't find it :-/ Also I don't have much experience in Android development, so if no one else suggest this approach, then I think kadmos could forget about it.
Brut.all said:
You identifies planets by ints (nameResId in this example - of course it must be unique), not by strings. Operations on ints are several times faster, than on strings, this is why Google decided to identify all things: resources, menu items, etc. just by ints.
Ahh and no, writing switch-cases to do something depending on given object isn't true OOP. OOP is above: enums know, which drawable is connected to them, there is no need for switches.
Click to expand...
Click to collapse
i know its not true oop i didnt want to have to do that but i have not yet seen a way to pass any value from a selected item into a method that could use that value to retrieve x amount of associated resources (images in this case).
Steven__ said:
Are all the images known from the beginning? In other words, is the user creating them at runtime or are you including them with your app? If they are included with your APK then normally, as Brut said, you should be able to use the identifiers directly.
...I can't really say anymore because I'm not fully following what you're trying to achieve.
Click to expand...
Click to collapse
using the planets example, say i had x (varying) amount of pics of each planet's surface in my drawables and wanted only those planet's pics to display in a grid view when a user selects whichever planet. thats really all this is. being new to this i just dont know the most efficient way to do it. if this was Flash i could just group all the images file names/paths in an external xml doc and use that to load them from whatever folder at runtime - i wouldnt need any of those 200 or so images declared as anything or even as assets in my library (and i would only need the xml because flash cant access a file system on its own to see and filter files that it would or wouldnt need based on, say, a string comparison - though there is third party software like Zinc that gives Flash that capability.)
so i did get this to work by passing a number as a tag (string) in a bundle though an imageView click event and then casting the string as an int to use in the switch - which as of now leads to one of 11 different int arrays of resources names (images) ive got declared in my ImageAdapter class to populate my gridView.
the way i wished i could made this work would have been to use a string (like a planet name) ,passed from whatever planet image/menu item/whatever was clicked, and use that string to compare and determine which and how many images in drawables were associated with that planet and then use that to create my gridView at runtime.
kadmos said:
so i did get this to work by passing a number as a tag (string) in a bundle though an imageView click event and then casting the string as an int to use in the switch - which as of now leads to one of 11 different int arrays of resources names (images) ive got declared in my ImageAdapter class to populate my gridView.
Click to expand...
Click to collapse
I see what you're saying, that makes sense. Just as a quick note though, if you're declaring your ImageViews programmatically, you don't have to use a string object for the tag. You can directly give the integer and then cast it back when you get the tag. Just remember to use (Integer) as the tag is actually just an unspecified object.
kadmos said:
the way i wished i could made this work would have been to use a string (like a planet name) ,passed from whatever planet image/menu item/whatever was clicked, and use that string to compare and determine which and how many images in drawables were associated with that planet and then use that to create my gridView at runtime.
Click to expand...
Click to collapse
Yes, I can see why you'd want to do it this way. Your current problem is that if you add more images, you have to manually update your arrays. Unfortunately I can't think of a better, 'clean' way of doing it.
@kadmos
Now I have problems understanding you ;-) But if you don't want to declare all images in sources, but in XMLs, then you could use XML arrays.
Code:
<resources>
<string-array name="planet_names">
<item>mercury</item>
<item>venus</item>
<item>earth</item>
<item>mars</item>
</string-array>
<integer-array name="planet_images">
<item>@array/mercury_images</item>
<item>@array/venus_images</item>
<item>@array/earth_images</item>
<item>@array/mars_images</item>
</integer-array>
<integer-array name="mercury_images">
<item>@drawable/mercury_0</item>
<item>@drawable/mercury_1</item>
<item>@drawable/mercury_2</item>
</integer-array>
<integer-array name="venus_images">
<item>@drawable/venus_0</item>
<item>@drawable/venus_1</item>
<item>@drawable/venus_2</item>
</integer-array>
<integer-array name="earth_images">
<item>@drawable/earth_0</item>
<item>@drawable/earth_1</item>
<item>@drawable/earth_2</item>
</integer-array>
<integer-array name="mars_images">
<item>@drawable/mars_0</item>
<item>@drawable/mars_1</item>
<item>@drawable/mars_2</item>
</integer-array>
</resources>
When user will open planet selector, you will iterate through contents of R.array.planet_names array, each item (planet) in this selector will have itemId set to array index. When user will click on something, you will get itemId of clicked item, then you will find array of its images as R.array.planet_images[itemId] (not exactly - it's conceptual example).
You will be able to add new images or even planets through XML editing.
Steven__ said:
Yes, I can see why you'd want to do it this way. Your current problem is that if you add more images, you have to manually update your arrays. Unfortunately I can't think of a better, 'clean' way of doing it.
Click to expand...
Click to collapse
Brut.all said:
But if you don't want to declare all images in sources, but in XMLs, then you could use XML arrays...
...You will be able to add new images or even planets through XML editing.
Click to expand...
Click to collapse
as i posted earlier this was the idea that i had - i havent tried it yet because i wanted to get some feedback from you guys just to see if i was completely off base. so right now its coming down to what would make for the more memory efficient final product - declaring all these images as class array constants (which i already have working) or using xml and coding the operations for parsing, counting, filtering, assigning, etc?
again thank you guys for your time and help
kadmos said:
or using xml and coding the operations for parsing, counting, filtering, assigning, etc?
Click to expand...
Click to collapse
Do you mean parsing XMLs? My example above uses standard Android resources. You don't have to parse these XMLs, you will get them as arrays And yes, it's super efficient, because they are compiled to easy-to-read form
ok then im going to go ahead and try it
be back soon
i hope

[Q] Html data parsing

Hi guys suddenly I have no experience with java and html parsing and I really need it...(possibly from http://www.uefa.com/teamsandplayers/teams/club=52280/domestic/index.html)
I want a simple way to convert an html website to xml document(fetch,convert,parse) or an easy alternative way to do it...
ps:if you know any alternative FREE resource of football(soccer) data tell it...
Android has built in several alternatives:
SAX Parser, XML Pull Parser and DOM.
A very good tutorial on all three of them can be found at IBM:
Working with XML on Android
I personally prefer the SAX Parser.
Of course you could just include another external parser, like HotSAX or many others via embedding external libraries.
In a small program I wrote I had to parse some html code, but the SAX Parser had problems with some German Umlauts. So I used TagSoup to clean the html and make a valid xml out of it. After that I could fetch the data with the SAX Parser.
thanks
wallla said:
Android has built in several alternatives:
SAX Parser, XML Pull Parser and DOM.
A very good tutorial on all three of them can be found at IBM:
Working with XML on Android
I personally prefer the SAX Parser.
Of course you could just include another external parser, like HotSAX or many others via embedding external libraries.
In a small program I wrote I had to parse some html code, but the SAX Parser had problems with some German Umlauts. So I used TagSoup to clean the html and make a valid xml out of it. After that I could fetch the data with the SAX Parser.
Click to expand...
Click to collapse
THANKS...maybe IBM example is good but a bit complicate for me(as i said i have no experience in parsing with java)...i prefer dom cause i get used to tree elements...
and if you can to publish some lines of source code with tagsoup(or refer me somewhere)...I NEED AN EXAMPLE
Here's a snippet of my program:
Code:
try {
URL url = new URL("http://www.url.com");
XMLReader xr = new Parser();
SAXHandler handler = new SAXHandler();
xr.setContentHandler(handler);
xr.parse(new InputSource(new InputStreamReader(url.openStream(), "ISO-8859-1")));
} catch (Exception e) {
}
You see, it's pretty simple...
The main part for "cleaning" the html-stuff is just this single line of code:
Code:
XMLReader xr = new Parser();
Parser() is of the type org.ccil.cowan.tagsoup.Parser;
If you want to use DOM, it should be quite similar.
Here's a basic example.

[Q] SOAP Call

Hello all... first time post in this area. Hope Im in the correct spot. I am trying to make a small app that will connect to a webservice. Typically we pass up a JSON object in C# to get a JSON return from our webservice.
Being new to Java, I am struggling with this. Any help would be much appreciated.
assume the following:
http://mysite/webservice.asmx
sO = "{"s_O":{"arPR":[{"sPSUniqueID":"K001"},{"sPSUniqueID":"K002"}]}}";
sUser = "mName";
sPassKey = "aqyu08ioa";
sMethodName = "mGetSummary";
our normal call in c# would look something like this...
JSONString = return androidSOAPCALL( methodName, sUser, sPassKey, sO);
Thanks for help on this guys...
Vic
You could do it that way: http://www.vogella.com/articles/AndroidJSON/article.html
However, you could also use a library made by Google which is calles Gson: http://code.google.com/p/google-gson/
It is much easier with the library.
A tutorial on this (points 3 and 4): http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
nikwen said:
You could do it that way: http://www.vogella.com/articles/AndroidJSON/article.html
However, you could also use a library made by Google which is calles Gson: http://code.google.com/p/google-gson/
It is much easier with the library.
A tutorial on this (points 3 and 4): http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Click to expand...
Click to collapse
Those are excellent articles on JSON. Thanks for the reference. I will most likely be using that to handle the returned JSON object.
However, my question is more concerning making my SOAP call to the asmx webservice. I cannot seem to relate any samples to my particular case as mentioned above.

newbie project.

Hi I want to make an app that loads a web page and auto refreshes.
Also I would like it to open any links and search for any text the user has asked to see. (I.e. the name of a secific town) And if found to either highlight that link on the displayed web page or open tabs for any instance.
It sounds simple but I have no idea where to start or what language to code in (although I was thinking java script).
Found a bunch of simple free web apps but would like to do it from scratch.
Any help would be great
Sent from my GT-N7100
The app needs to be written in Java.
However, you can load a html page into a WebView and do the actual coding in the web page you display. That enables to do the simplest things in JavaScript.
If you want, send me your html file and I will create an app for you which displays this page.
But it will be a better learning experience if you do it yourself.
Thanks for the reply but the web page I have in mind is a news page for the fire service I work for.
It has links to incidents with more details. I wanted to load the page and search each of the links for my stations.
That way my wife only has to check her phone to see where I am.
I would love to do it all myself. Have some experience in vb but im not sure if you can program in that for android.
Plus I really want to learn java lol and I learn better by doing. If you can give me some pointers I would be very happy
Sent from my GT-N7100
jaffa1980 said:
Thanks for the reply but the web page I have in mind is a news page for the fire service I work for.
It has links to incidents with more details. I wanted to load the page and search each of the links for my stations.
That way my wife only has to check her phone to see where I am.
I would love to do it all myself. Have some experience in vb but im not sure if you can program in that for android.
Plus I really want to learn java lol and I learn better by doing. If you can give me some pointers I would be very happy
Sent from my GT-N7100
Click to expand...
Click to collapse
Look at the methods of WebView.
The text it searches for, is it all on that one html page? Can it find the identifying info in the URL of each link or will it need to actually load each linked page and search for the text in those?
Sent from my SAMSUNG-SGH-I727 using xda app-developers app
It would need to open each link in order to search I think.
Although it doesnt need to be made visible unless it matches the search criteria... I know that with the vb I could open a link in a new page but keep its visibility zero while it is manipulated... is that possible in java script?
Sent from my GT-N7100
jaffa1980 said:
It would need to open each link in order to search I think.
Although it doesnt need to be made visible unless it matches the search criteria... I know that with the vb I could open a link in a new page but keep its visibility zero while it is manipulated... is that possible in java script?
Sent from my GT-N7100
Click to expand...
Click to collapse
That makes it a bit harder. I don't know javascript very well but either way, I would do it using the Java WebView class to load the links. I have written an app once that used the JSoup library to download and parse the needed needed info from links, assemble them into a small summary page and displayed with WebView. I don't know if thats the best way but it worked for me...
You could search the html file for "<a href".
You basically want a web crawler^^
Regular expressions would be the right place to look, +1 for @nikwen 's answer.
I would basically do something like an edit text where the user inputs the words he wants to find on the webpage, and use regex to parse all <a href / </a> tags in a given html page which the app would previously have downloaded.
You could do something like this to parse the links (this will give you everything between ) :
Code:
File file = new File("/sdcard/index.html");
if(file.exists()) {
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
if (line.contains("<a href")
line.replaceAll("(<a href=\")(.+)(\")(>.*</a>)", "$1$3$4");
text.append(line);
text.append('\n');
//the text variable will contain all the links on your page by the end of the loop
}
}
catch (IOException e) {
}
You could then assign the text variable to a TextView which would have the android:autoLink="web" flag, thus displaying all links to the user
Thanks very much for your input, although i'm extremely new to java so its taking some time to get my head around the whole thing lol.
I have recently found Basic4Android which is very similar to Visual Basic. I have managed to pick that up a bit quicker than Java.
I was wondering if it is a popular language on here and if there are many people i may be able to ask specific questions re my app?
I have managed to "map out" what i want and even start the programming having designed several pages.
The thing i wondered about this is...
Once a web page is loaded are you able to programatically (is that a word?? ha ha) open links found on that web page (for me to search for a station).
the search string I am using, the user selects from a Spinner (drop down box) that i have populated.
once i have the links that have the mentioned stations i can use a tab view to have the main page and all incidents open together.
jaffa1980 said:
Thanks very much for your input, although i'm extremely new to java so its taking some time to get my head around the whole thing lol.
I have recently found Basic4Android which is very similar to Visual Basic. I have managed to pick that up a bit quicker than Java.
I was wondering if it is a popular language on here and if there are many people i may be able to ask specific questions re my app?
I have managed to "map out" what i want and even start the programming having designed several pages.
The thing i wondered about this is...
Once a web page is loaded are you able to programatically (is that a word?? ha ha) open links found on that web page (for me to search for a station).
the search string I am using, the user selects from a Spinner (drop down box) that i have populated.
once i have the links that have the mentioned stations i can use a tab view to have the main page and all incidents open together.
Click to expand...
Click to collapse
Since Java and C++ are the native languages for Android apps, there will not be that much devs which can help you.
Learn Java. It is a great language.
---------- Post added at 01:23 PM ---------- Previous post was at 01:22 PM ----------
And to open links programmatically: There is a method in WebView to load a page. Just load the new one.
If you're using a webview then any familiarity with JavaScript will help. You can find and download all the links on a page using Ajax, quite easily. For example, if you included the jQuery library in your page then you could do the following...
HTML:
$("a").each(function() {
$.ajax({
url: $(this).attr("href");
success: function(data) {
// do something with "data" here - it's a linked page
}
});
});
But, if you're familiar with basic then B4A may be the way for you to go. You'll need to learn how to inspect the DOM (document object model) which is the code behind a webpage.
Also, Stack Overflow is invaluable for quick help from experienced developers. A combination of this place and that (http://stackoverflow.com/questions/tagged/basic4android) will certainly get you going in the right direction. Just a word of warning.... If you're going to post on Stack Overflow then don't just ask "how do I do this?" It doesn't go down well. Show them what you have done and tell them what you want to achieve. They like people who try for themselves first
Good luck.
You definitely need Jsoup here! It is a very fast and easy to use (java) library.
Take a look at this code:
Code:
String URL = "yoururl";
String linkText;
String location;
Boolean stationFound;
ArrayList<String> locations = new ArrayList<String>();
// Get all links to incidents
Document doc = Jsoup.connect(URL).get();
// You have to take a look at the HTML and get some unique attribute to get all the incidents
// This can be a class/id/div etc.
Elements IncidentLinks = doc.select("a[class=someClassToSelectIncidents]");
// Now loop through all incident links
for(IncidentLinks : link){
[INDENT]
// Get the URL of the current detailed page
URL = link.attr("href");
// This is the more detailed page
doc = Jsoup.connect(URL).get();
// Get the HTML of the detailed page
String pagecontent = doc.toString();
// If your station is named in this HTML add it to the locations array
if(pagecontent.contains("Yourstation")){
[INDENT]
// Get the current location
// You need to find it yourself, take a look at the HTML
// It could be something like:
location = doc.select("div[class=location]").text();
// Add the location to the array
locations.add(location);[/INDENT]
}
[/INDENT]
}
// Now you have an array with all locations where your station is at the moment
// I dont know if your station can be at multiple locations at once
// You could loop through them and add them to a textview.
Thanks for all your advice i am starting to learn java, i have got eclipse and am watching all the video lessons i can grab lol i have a couple of books and a complete reference book 8...
I was wondering if i needed to reference this jsoup library before i can use it in eclipse.
Please be patient with me lol i have 2 jobs 3 kids and a prego wife so can be.... well, skatty is a polite way of putting it!
You just have to load the library into your project, it's very easy, just google it. If you have some programming experience you'll learn jav pretty fast. Good luck!
Sent from my NexusHD2 using xda app-developers app
jaffa1980 said:
Thanks for all your advice i am starting to learn java, i have got eclipse and am watching all the video lessons i can grab lol i have a couple of books and a complete reference book 8...
I was wondering if i needed to reference this jsoup library before i can use it in eclipse.
Please be patient with me lol i have 2 jobs 3 kids and a prego wife so can be.... well, skatty is a polite way of putting it!
Click to expand...
Click to collapse
Copy it into the libs folder of the project and it will be linked automatically.
Hi,
Still studying away i was wondering if there is a resource webpage i can download other projects to open in eclipse so that i can explore all the bits I'm reading about.
When i did VB i would often look at other projects with similar components as mine so i could get an idea on how to implement things properly.
I am getting the hang of the eclipse environment but there is so much more to the project area (resource files, xml and java etc).
Does anyone have any recommended sites for tutorials books for reading etc?
Cheers guys
jaffa1980 said:
Hi,
Still studying away i was wondering if there is a resource webpage i can download other projects to open in eclipse so that i can explore all the bits I'm reading about.
When i did VB i would often look at other projects with similar components as mine so i could get an idea on how to implement things properly.
I am getting the hang of the eclipse environment but there is so much more to the project area (resource files, xml and java etc).
Does anyone have any recommended sites for tutorials books for reading etc?
Cheers guys
Click to expand...
Click to collapse
I'm obviously assuming you have the Android SDK installed, so just look in there for the samples folder. It's full of demo apps to rip apart and see how they were made
ok im right fed up!!
i havent had time to be able to read or study or anything!!
would someone throw me a bone and upload a template based on the description i gave earlier... i know its freakin lazy but im having a **** ol time of it recently (health issues... hospital time etc) and could really do with the help...
the url of the newsdesk is
http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35
and as i said just the option of either viewing the page directly or if selected a station from a drop down box the main url shown on the first tab with other tabs populated by incidents with the selected station preferably from todays incidents. (see link)
i would consider this to be my luck turning and could really do with it
thanks
jaffa1980 said:
ok im right fed up!!
i havent had time to be able to read or study or anything!!
would someone throw me a bone and upload a template based on the description i gave earlier... i know its freakin lazy but im having a **** ol time of it recently (health issues... hospital time etc) and could really do with the help...
the url of the newsdesk is
http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35
and as i said just the option of either viewing the page directly or if selected a station from a drop down box the main url shown on the first tab with other tabs populated by incidents with the selected station preferably from todays incidents. (see link)
i would consider this to be my luck turning and could really do with it
thanks
Click to expand...
Click to collapse
Thanks for the link, now its more clear what you want.
At first take a look at the HTML of the webpage.
All incidents are listed per day as you can see in this image. The incidents are wrapped inside a DIV tag:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
When we take a closer look, the link of an incident is stored in this TD tag.
So now we know the structure of the webpage we need to itterate through all the TD tags with the links in it.
If you look even closer you can see all the TD tags are inside a TABLE tag (ORLY??), BUT with the same class: class="tabularData".
The path to each page looks like this TABLE(class="tabularData") > TR > TD(first) > A
When you've got all the links you can visit the page and extract the word(s) you are looking for.
You can do this in two ways:
Use PHP/javascript/whatever to create your own website which displays the data. Then use WebView to display it in your own app.
Use Java to find the data and show it in an android layout (listview).
In PHP you want to use XPATH (http://stackoverflow.com/search?q=xpath)
In Java you want to use JSOUP (http://stackoverflow.com/search?q=JSOUP)
In Android it looks something like this:
HTML:
// Get the content of page
Document doc = Jsoup.connect("http://www.dsfire.gov.uk/News/Newsdesk/IncidentsPast7days.cfm?siteCategoryId=3&T1ID=26&T2ID=35").get();
// Select each day, this TABLE tag is inside the DIV tag you see in the first image.
Elements days = doc.select("table[class=tabularData]");
// Loop through each day
for(Element day : days){
// Select each incident
Elements incidents = day.select("tr");
// This loops through all URL's of the incidents.
for(Element incident : incidents){
// And get the url of the page to get more details of the incident
String url = incident.select("td").first().select("a")..attr("abs:href");
// Now get the content of the detailed page
String incidentpage = Jsoup.connect(url).get().toString();
// And check wether the page contains the string you are looking for
If(incidentpage.contains("Stringyouarelookingfor"){
// Do something
}
}
}
This is just a very rough piece of code, and it might not (probably doesn't) work. But I hope you get the idea and can go on by yourself.

Registering devices with GCM error

Hello everybody,
I'm experimenting with Google Cloud Messaging and I was following the tutorial at https :// github. com/ GoogleCloudPlatform/gradle-appengine-templates/blob/master/GcmEndpoints/README.md (I beg pardon but I can't post links). Unfortunately I spent the last two days struggling with solving all the dependencies but I still miss one.
In this code (taken from the tutorial at point 2.2) I cannot resolve the class "Registration"; I've been trying looking for it but given such a vague name I got back millions results that had little to do with my problem.
I also tried to add to my project all the suggested libraries but none of them contained the class I need.
Code:
public GcmRegistrationAsyncTask() {
Registration.Builder builder = new Registration.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
// Need setRootUrl and setGoogleClientRequestInitializer only for local testing, otherwise they can be skipped
.setRootUrl("h t t p : / / 10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
// end of optional local run code
regService = builder.build();
}
Could somebody point me in the right direction?
Thanks in advance for the help!
Bye
Mario
I was just following the same instructions today and I am experiencing the same problem unfortunately.
thvranken said:
I was just following the same instructions today and I am experiencing the same problem unfortunately.
Click to expand...
Click to collapse
Well, at least it means probably we are not dumb
At any rate if I should find something I'll post it here so it's available to whoever needs it and pls do the same...
Hmm.. I couldn't find a "Registration" class or import anywhere in their source code so it may be included in one of the dependencies, try checking your build.gradle files against the one given in their source code to check for the correct dependencies.
In any case, their method is (IMO) an extremely weird and complicated way of doing something very simple, if you haven't found the solution by Monday (when I should have more free time) I'll put together a package (including the html/php/MySQL server back-end files) with how I managed to do GCM messaging (which includes multicast messages)
Jonny said:
Hmm.. I couldn't find a "Registration" class or import anywhere in their source code so it may be included in one of the dependencies, try checking your build.gradle files against the one given in their source code to check for the correct dependencies.
In any case, their method is (IMO) an extremely weird and complicated way of doing something very simple, if you haven't found the solution by Monday (when I should have more free time) I'll put together a package (including the html/php/MySQL server back-end files) with how I managed to do GCM messaging (which includes multicast messages)
Click to expand...
Click to collapse
Hi Jonny, tnx for taking time and having a look! I checked the gradle file but it seemed everything in order...
At any rate I started studying better the code (I should have done it earlier actually) and noted that in the backend Android Studio created before a class with the methods I invoke in the app and before the declaration there is annotation:
Code:
@Api(name = "registration", version = "v1", namespace = @ApiNamespace(ownerDomain = "backend.rtalert.rt_finance.com", ownerName = "backend.rtalert.rt_finance.com", packagePath=""))
public class RegistrationEndpoint {
private static final Logger log = Logger.getLogger(RegistrationEndpoint.class.getName());
Now, searching on the web seems like I would somehow need to compile the backend module and import it as a lib into my app (despite is all within the same project). I imported both the zip file and the discovery one and then synced gradle, but still nothing.
Tomorrow after a few hours sleep I'll try again...
Good night
Mario
Ok apologies for being a bit late, life's been busier than I was expecting over the last few days...
Anyway, the web server files are all located in the following zip file >>> http://darkforest-roms.co.uk/Misc/AndroidGoogleColudMessaging.zip
There is also a table.txt file that contains the MySQL database base table I'm using - the setup is for an app for my school where the staff can send messages to individual year groups or to all year groups.
Assuming whatever webhosting service you are using has phpMyAdmin or similar you can just change the table columns in the table.txt file and import it. Then you need to go through register.php, update.php, possibly unregister.php and index.php to modify the website code to your specific application.
You will also need to edit the parameters in config.php for your Google API key and database name, user and password.
Thats it for the website side code, give me a couple of days to sort out the app-side java code, I need to modify some stuff in my current code before uploading it
Jonny said:
Ok apologies for being a bit late, life's been busier than I was expecting over the last few days...
Anyway, the web server files are all located in the following zip file >>> [
There is also a table.txt file that contains the MySQL database base table I'm using - the setup is for an app for my school where the staff can send messages to individual year groups or to all year groups.
Assuming whatever webhosting service you are using has phpMyAdmin or similar you can just change the table columns in the table.txt file and import it. Then you need to go through register.php, update.php, possibly unregister.php and index.php to modify the website code to your specific application.
You will also need to edit the parameters in config.php for your Google API key and database name, user and password.
Thats it for the website side code, give me a couple of days to sort out the app-side java code, I need to modify some stuff in my current code before uploading it
Click to expand...
Click to collapse
Hi Jonny, don't worry, I was quite busy these days myself, plus I really appeciate your help... In the weekend I'll set it up so I can test it and let you know...
Thanks again
Mario

Categories

Resources