Android :: Using SAXParser To Get List Of Strings To Arraylist

Jun 21, 2010

I'm trying to parse a web service response using SAXParser and get certain values, store them into an arraylist, then display them in a listview. Example XML being returned from web service:
<ArrayOfStrings>
<string>value</string>
<string>value</string>
<string>value</string>
</ArrayOfStrings>

Here is my SAXHandler class:
public class SAXHandler extends DefaultHandler {
private boolean outertag = false;
private boolean mytag = false;
ArrayList<String> alist;
public ArrayList<String> getList(){
return this.alist;
} @Override
public void startDocument() throws SAXException {
} @Override
public void endDocument() throws SAXException{
} //@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if (localName.equals("ArrayOfString")){ this.outertag=true;
}else if (localName.equals("string")){ this.mytag=true;
} }
@Override
public void characters(char ch[], int start, int length) {
if (this.mytag){ String s = new String(ch, start, length);
alist.add(s);
} }
@Override public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if (localName.equals("ArrayOfString")){ this.outertag=false;
}else if (localName.equals("string")){ this.mytag=false;
} } }

And here's my method calling and returing my web service:
try{ URL url = new URL("xxxxxxx");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
SAXHandler SAXHandler = new SAXHandler();
xr.setContentHandler(SAXHandler);
xr.parse(new InputSource(url.openStream()));
ArrayList<String> stateList = SAXHandler.getList();
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stateList);
RESTlist.setAdapter(aa);
I keep getting an exception and I don't know why. Can anyone give me a hand?

Logcat:
06-21 20:10:47.901: ERROR/AndroidRuntime(472): Uncaught handler: thread main exiting due to uncaught exception
06-21 20:10:47.950: ERROR/AndroidRuntime(472): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pa.passammain/com.pa.passammain.RESTtest}: java.lang.NullPointerException: println needs a message
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.os.Looper.loop(Looper.java:123)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.app.ActivityThread.main(ActivityThread.java:4203)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at java.lang.reflect.Method.invoke(Method.java:521)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at dalvik.system.NativeStart.main(Native Method)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): Caused by: java.lang.NullPointerException: println needs a message
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.util.Log.println(Native Method)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.util.Log.e(Log.java:208)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at com.pa.passammain.RESTtest.onCreate(RESTtest.java:82)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
06-21 20:10:47.950: ERROR/AndroidRuntime(472): ... 11 more

After editing how my error is displayed in my try/catch i'm not longer getting an exception, but just a blank screen. Here is what my logcat is displaying:

06-21 20:22:00.330: WARN/System.err(542): java.lang.NullPointerException
06-21 20:22:00.352: WARN/System.err(542): at com.pa.passammain.SAXHandler.characters(SAXHandler.java:45)
06-21 20:22:00.352: WARN/System.err(542): at org.apache.harmony.xml.ExpatParser.text(ExpatParser.java:166)
06-21 20:22:00.362: WARN/System.err(542): at org.apache.harmony.xml.ExpatParser.append(Native Method)
06-21 20:22:00.371: WARN/System.err(542): at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:506)
06-21 20:22:00.371: WARN/System.err(542): at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:467)
06-21 20:22:00.371: WARN/System.err(542): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:329)
06-21 20:22:00.381: WARN/System.err(542): at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:286)
06-21 20:22:00.381: WARN/System.err(542): at com.pa.passammain.RESTtest.onCreate(RESTtest.java:71)
06-21 20:22:00.391: WARN/System.err(542): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
06-21 20:22:00.391: WARN/System.err(542): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
06-21 20:22:00.401: WARN/System.err(542): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
06-21 20:22:00.401: WARN/System.err(542): at android.app.ActivityThread.access$2100(ActivityThread.java:116)
06-21 20:22:00.410: WARN/System.err(542): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
06-21 20:22:00.410: WARN/System.err(542): at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 20:22:00.420: WARN/System.err(542): at android.os.Looper.loop(Looper.java:123)
06-21 20:22:00.420: WARN/System.err(542): at android.app.ActivityThread.main(ActivityThread.java:4203)
06-21 20:22:00.420: WARN/System.err(542): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 20:22:00.430: WARN/System.err(542): at java.lang.reflect.Method.invoke(Method.java:521)
06-21 20:22:00.430: WARN/System.err(542): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-21 20:22:00.442: WARN/System.err(542): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
06-21 20:22:00.442: WARN/System.err(542): at dalvik.system.NativeStart.main(Native Method)

Android :: Using SAXParser to get List of Strings to Arraylist


Android :: Accsess Strings In List View?

Oct 14, 2010

Code...

I don't know how to get the right element by onItemClick. the var i is always -1

View 2 Replies View Related

Android :: How To Pull Strings From Array For Clicked List Item?

Aug 4, 2010

I have an array adapted listview (the array adapting is done in another class). I just got the click listener working for the list but now I want set it up so that when I click an item. It pulls the strings from the clicked item and piggybacks them on the intent to a new activity. I figure I am supposed to use intent.putextra however I am not sure how to pull the correct strings corresponding to the item that i click on. My code is below. I am simply lost to be honest

//Initialize the ListView lstTest = (ListView)findViewById(R.id.lstText);
//Initialize the ArrayList alrts = new ArrayList<Alerts>();
//Initialize the array adapter notice with the listitems.xml layout
arrayAdapter = new AlertsAdapter(this, R.layout.listitems,alrts);
//Set the above adapter as the adapter for the list
lstTest.setAdapter(arrayAdapter);
//Set the click listener for the list
lstTest.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView adapterView, View view, int item, long arg3) {
Intent intent = new Intent( HomePageActivity.this, PromotionActivity.class );
finish(); startActivity(intent); } } );

my alerts class..
public class Alerts {
public String cityid;
public String promoterid;
public String promoshortcontent;
public String promocontent;
public String promotitle;
public String locationid;
public String cover;

@Override public String toString() {
return "City: " +cityid+ " Promoter: " +promoterid+ "Short Promotion: " +promoshortcontent+ "Promotion: " +promocontent+ "Title: " +promotitle+ "Location: " +locationid+ "Cover: " +cover+ "$"; } }

anddddd my alertsadapter class..
public class AlertsAdapter extends ArrayAdapter<Alerts> {
int resource;
String response; Context context;
//Initialize adapter public AlertsAdapter(Context context, int resource, List<Alerts> items) {
super(context, resource, items); this.resource=resource;
} @Override public View getView(int position, View convertView, ViewGroup parent)
{ LinearLayout alertView; //Get the current alert object Alerts al = getItem(position);
//Inflate the view if(convertView==null) {
alertView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, alertView, true); } else {
alertView = (LinearLayout) convertView; }
//Get the text boxes from the listitem.xml file
TextView textPromo =(TextView)alertView.findViewById(R.id.txtPromo);
TextView textPromoter =(TextView)alertView.findViewById(R.id.txtPromoter);
TextView textLocation =(TextView)alertView.findViewById(R.id.txtLocation);
//Assign the appropriate data from our alert object above
textPromo.setText(al.promocontent); textPromoter.setText(al.promoterid);
textLocation.setText(al.locationid); return alertView;
} }

View 2 Replies View Related

Android :: Strings Generated By Substring Not Treated As Hard Coded Strings

Nov 21, 2010

Dear all, I'm making a simple file decoder for Android 2.2 that needs to find the filename of the encoded file from a header. This filename should then be used as the filename for the decoded file (as you would expect).The filename is identified by the substring name=, so the actual name starts 5 places after that. The line is read by a BufferedReader and temporarily stored in currLine.For some reason I can't understand nor find on the web, Strings do not always seem to be Strings.Gives no output file, nor an IO exception. The string is parsed properly though: from the System.out debugging lines.

View 1 Replies View Related

Android :: Most Speed-efficient Way To Hard Code Map Of Strings To Strings?

Aug 19, 2010

I've got a map of about 500 entries. Strings are all very short (less than 5 chars).Its read-only data I want to read once at app-startup. What's the best way to store this so that loading this data is fast?I've tried storing the data in a CSV in res/raw but this takes about 700ms to parse on my N1, so I'm hoping there's a faster way.

View 8 Replies View Related

Android :: Externalize Strings In Source Files To Strings.xml Automatically?

Sep 20, 2009

Is there a way to externalize all the strings in the source files to strings.xml automatically?

View 2 Replies View Related

Android :: References To Other Strings In Strings.xml?

May 3, 2010

Is it possible to reference other strings inside of strings.xml ?

Something of the form...

(If it did exist, there would of course be problems of circular, infinite definitions, etc. to beware of).

View 1 Replies View Related

Android :: Can One Combine Android Resource Strings Into New Strings?

Aug 31, 2010

by which the resource "bar" becomes an alias for the resource named "foo".What I would for my app is a possibility to combine an existing resource prefix with different suffixes,where the resource "bar" would yield the string "foobar". Its clear that '+' doesn't work here but is there some other option to achieve such a string concatenation, so that one could define a bunch of string resources that have a common prefix?I realize of course that I could do such resource string concatenation at runtime but defining them statically in the resources would seem so much more elegant and simpler.

View 1 Replies View Related

Android :: SAXParser With POST To URL

Jan 22, 2009

I have an API. I'm using that requires me to POST instead of use GET to submit my credentials before receiving back my XML response. I've parsed a bunch of various XML services like this, but this is the first time I've run across having to post to the URL. Basically they want a username and password parameter submitted as a post to the URL before the XML is fed back. Does anyone have a working example, or maybe able to point me in the right direction?

View 3 Replies View Related

Android :: How To Pause SAXParser?

Aug 27, 2010

I'm using SAXParser to parse large xml document from the net. And sometimes I need to pause SAXParser. I know how to stop parser by using SAXException, but how I can paused it? May be can I stop parser and then set up it to pass already parsed tags?

View 2 Replies View Related

Android :: SAXParser And Malformed XML

Aug 14, 2010

I am using a saxparser to read RSS XML from the web and populate listviews. Everything works fine until I come across malformed XML, usually encoding related, and my reader falls over. I have changed it so that it gets as many items parsed and populated before falling over (by default if it finds a malformed XML, then it will give up and return an empty collection). The problem is, the next time I try and view that feed (or any other feed), it still has the contents after the malformed bit hanging around.

For example, the first time it now shows all the items upto the malformed item, if I refresh, it will only show items after, refresh again, it will show items upto and so forth. The weird thing is, I create a new instances of everything every time. So I don't understand why stuff is handing around. It's like there is some read pointer, or connection that needs to be closed and reset, but I can't for the life of me work out what or where!

My GetFeed method:
private RSSFeed getFeed(String urlToRssFeed) {
SAXParserFactory factory;
SAXParser parser; XMLReader xmlreader;
URL url; InputSource is;
try { url = new URL(urlToRssFeed);
factory = SAXParserFactory.newInstance();
factory.setValidating(false);
parser = factory.newSAXParser();
xmlreader = parser.getXMLReader();
xmlreader.setContentHandler(m_FeedHandler);
is = new InputSource(url.openStream());
xmlreader.parse(is); m_ErrorCode = NOERROR;
} catch (SAXParseException e) { e.printStackTrace();
m_ErrorCode = XMLFORMAT;
} catch (Exception e) { e.printStackTrace();
m_ErrorCode = CONNECTION;
} finally { is = null;
url = null; parser = null; factory = null;
return m_FeedHandler.getFeed();
//May be complete, partial or empty.
} }

View 2 Replies View Related

Android :: SAXParser Fails On Some RSS Feeds

Feb 28, 2009

I'm using the SAX parser to read some RSS feeds and have found a problem. Some feeds, for example CNN Money Top Stories, have embedded some characters in their content, i.e. the copyright symbol. Well, that's not valid XML and the SAX Parser fails with an exception "invalid token". The only help I have seen given is to fix the XML at the source and that's not an option obviously. So, I can think of 2 options and they both stink:

(a) read the content first, scrub it, and then pass it to the parser.
(b) Use DOM instead of SAX.

What I *want* to do is make the parser a little more forgiving and just accept or discard/ignore the bad text. I'm not have any luck with setErrorHandler. My error handler does not get called.

View 7 Replies View Related

Android :: Youtube API - SAXParser Error

Jul 22, 2010

I have been trying to use the youtube APIs on the Android Platform (version 1.6) and have been stuck with the SAXParser whenever I send out a query from the following line of code:
VideoFeed videoFeed = service.query(query, VideoFeed.class);

I initially had errors with the SAXParser for which I applied the patch suggested here:
https://code.google.com/p/android/issues/detail?id=9493

After this, the previous error that I had which called an ExternalParametersEntities exception was resolved but I now get a Verify Error:
com.google.gdata.util.common.xml.parsing.SecureGenericXMLFactory $SecureSAXParserFactory

Would anyone know how to get around this error? Is there any other way to use the youtube API query on an Android platform?

View 3 Replies View Related

Android :: SAXParser Getting Stuck On Certain Characters

Jan 26, 2009

I'm using a SAXParser to parse an XML document and its getting stuck on certain symbols like the 'trademark' symbol and I think even double- quotes". I really don't need these characters so it would be fine if the parser just skips over these. Instead it throws an exception and quits parsing the document. What can I do?

View 7 Replies View Related

Android :: SAXParser Invalid Token Exception And Attribute Value

Jan 1, 2010

I am using SAXParser to parse an html page (any better solution?) and have this exception:
W/System.err( 1358): org.apache.harmony.xml.ExpatParser $ParseException: At line 1, column 59: not well-formed (invalid token)
I have reduced the page to this:
<div id="submenu"><a href="/compte/console.pl? id=382730&idt=1cf6b94aa1a4cf84"></a></div>
And what causes the exception is the '&' inside the href attribute value.

Here is a minimalist test code:
DefaultHandler emptySaxHandler = new DefaultHandler() {};
String xmlstr = "<div id="submenu"><a href="/compte/console.pl?id=382730&idt=1cf6b94aa1a4cf84"></a></div>";
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new ByteArrayInputStream(xmlstr.getBytes ()),emptySaxHandler);

Is this a normal behaviour or kind of bug? If normal, what should do to preprocess the string before parsing?

View 3 Replies View Related

Android :: Using Android SAXParser - One My XML Elements Is Breaking In Half

Mar 30, 2010

Im using the SAXParser object do parse the actual XML.

This is normally done by passing a URL to the XMLReader.Parse method. Because my XML is coming from a POST request to a webservice, I am saving that result as a String and then employing StringReader / InputSource to feed this string back to the XMLReader.Parse method.

However, something strange is happening at the 2001st character of the XMLstring. The 'characters' method of the document handler is being called TWICE in between the startElement and endElement methods, effectively breaking my string (in this case a project title) into two pieces. Because I am instantiating objects in my characters method, I am getting two objects instead of one.

This line, about 2000 chars into the string fires 'characters' two times, breaking between "Lower" and "Level"

<title>SUMC-BOOKSTORE, LOWER LEVEL RENOVATIONS</title>

When I bypass the StringReader / InputSource workaround and feed a flat XML file to XMLReader.Parse, it works absolutely fine.

Something about StringReader and or InputSource is somehow screwing this up.

Here is my method that takes and XML string and parses is through the SAXParser.

CODE:.............

How to not have 'characters' firing off twice when I get to this point in the XML String.

Or, show me how to use a POST request and still pass off the URL to the Parse function.

View 3 Replies View Related

Android :: Implementing Characters Method Using SAXParser On Android?

Feb 8, 2010

I am parsing xml using the SAXParser and want to know if this is the right way to implement the characters method. Assume there's a class-level String variable named elementValue and it is initialized to "" in the startElement method.

Here is the characters method:
@Override
public void characters(char[] ch, int start, int length) {
String charsToAppend = new String(ch, start, length);
elementValue = elementValue + charsToAppend;
}
Is it correct that we need to append to the value we have so far and is the append being done correctly? Also, why does the characters method give the start index? Won't it be zero every time?

View 2 Replies View Related

Android :: Cannot Read Two Different Tags Using SAXParser In Android?

Sep 23, 2010

I am using SAXParser to parse an XML document having three different tags. But in the result I get the value of only "one tag and not Other two tags. Can someone explain?
My XML is :
<outertag>
<innertag>hello</innertag>
<othertag>good morning</othertag>
<thirdtag Attribute="its a pleasant day"/>
</outertag>

View 1 Replies View Related

Android :: External Strings.xml

Feb 8, 2010

Is it possible to have an external strings.xml or something like that?I'd like to translate my application into several different languages, but the apk would become to large if I include all languages.I would instead like to be able to load languages on demand (from an external URL) and store them on the SD card.Is that possible in any way? How could I in that case use the new language?

View 5 Replies View Related

Android :: XML Syntax For Strings?

Oct 8, 2010

In my XML values strings code, I have a string with some text that I want to have only a few of the words in color. I also want to have a new line for some of the text.

I can do things like: < b> my bold text < /b> and that works (leading space added for this post).

I try to tag the text with color but no color shows up and there are no errors.

CODE:.............

I also try adding a new line - I've tried things like:
< LF> some text, space at beginning only for this post < /LF>
< CR> this didn't work either < /CR>
< p> some text, space at beginning only for this post < p>

But, no new paragraph / new line, carriage rtn happens.

View 1 Replies View Related

Android :: Loop Through Strings.xml?

Jul 2, 2009

Is there a way to loop through the values of strings.xml?

View 2 Replies View Related

Android :: Modify Strings.xml Dynamically

Apr 21, 2010

I want to implement a "Settings" section in my application, and I want that when the user selects whatever, one of the strings from string.xml will change its value according to the user selection. Is this possible at all? any ideas on how to implement it?

View 4 Replies View Related

Android :: Creating Strings And Arrays

Sep 29, 2010

I am trying to create a information based application, I have 92 subjects, for these subjects I have 92 map locations and maybe as much as 400 url links. Currently I have created a class for each map location and a class for each url link, but this would mean I would have in excess of 500 classes, so what I want to do is split the subjects in four catergories ie. listview of about 23 in four tabs which I have done and create a class that gets the url based on the selection the use makes and do the same for the map locations.Do I need to put these in a xml using the id method and how do you call individual entries, i.e. would I need 500 Android:id is there an easier way?

View 2 Replies View Related

Android :: Java101 Strings And Ints

Oct 29, 2010

Im new to android and java but moving along at full speed. My lack of experience is making some elementary tasks too repetitive.For example I want to be able to loop through the following snipit of code the problem is I want "i" replace the number "1" in g1m0+=1; so through each iteration g1m0 would become g2mo.I know this is a simple to the novices out there can you please give me some insight to shorten up my code here..

View 9 Replies View Related

Android :: Comparing Two Strings Get False

Jun 29, 2010

I have a strange error that I can't understand. I have this code: if (VersionsKoll == Ver) { }

View 5 Replies View Related

Android :: Few Strings Which Need To Translate And Display

Aug 21, 2010

I have a few strings which I need to translate and display.Those strings are in variables. I have the translation in the strings.xml file.

View 1 Replies View Related

Android :: How To Handle Thousands Of Strings ?

Oct 4, 2010

Maybe some of you has an application with quotations or jokes or something. I would like to make a similar. So how can I get the i. element of my string array (which is in arrays.xml)? I don't want to load the whole array in a String[], because it can be too big. Or it's not a good idea to put them into arrays.xml? How do you handle thousands of strings?

View 24 Replies View Related

Android : How To Get All Nodes Content From Strings.xml?

Feb 12, 2010

I would like to write a program to parse the nodes of strings.xml and then get the content and attribute"name". Can anyone has experience on it?

View 3 Replies View Related

Android :: How To Display An ArrayList?

Sep 9, 2009

I'm new to android developing and I've done a function which return an arraylist type ArrayList<ArrayList>. I would like to display this arraylist for see if my function work.

View 11 Replies View Related

Android :: Efficient Mapping Of Strings To Ints

Dec 29, 2009

Currently I'm using a HashMap to map the strings to int and the int values need to be frequently accessed. I'm looking for a better way to do this if possible with minimal object creation, and preferable being able to store the values as primitive ints without wrapping them with the Integer class. (Basically, the reverse of the SparseArray's int->object mapping.)

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved