Android getting multiline strings from XML problems - android

I am trying to print a text which is fetched from the server. What is the best way to escape all special characters and print safely?
Because the string which is fetched from the server is entered by user an stores it on database. So there is a possibility use <?php ?> , & etc which may cause errors. I have tried < > which solved this problem.
But when setText() the string to an EditText the string gets truncated after &
So I need a best solution in which the text entered by the user will save safely in the database and retrieve the multi-line string with special characters safely.
What is the best way to do this?

I think you should use StringBuilder instead of simply reading the text from XML.
Follow these steps :
1) For each new tag in XML (in startElement) , create a String builder
2) Append the text to same StringBuilder in reading from Character method.
3) At last, assign that StringBuider to some String at the End of the tag (in EndElement).
Hope this will give you some idea to solve the problem .
Try ...!!!

Anyways the problem solved by using URLDecode.decode() at the app and urldecode(),stripslashes() at the server side.
Dont know whether this is the perfect solution, but it worked for me.

Related

having unneeded spaces between lines

I am writing my data in a database and getting it in android to set it in a textview but when I write for example:
"hello,
how are you?"
which means each line on a row if the row in the phone completes I get an extra line space and I don't want this need to remove it how can I do that?
thanks.
EDIT:
how can I place a line space in an arabic database: I added \n didn't work "\n" didn't work '\n' didn't work!! any ideas?
even if the database wasn't arabic that didn't work so can I place the \n in the database in a way that the android program takes it as a line space?
Edit2
Is there any other method? other than the one that I've mentioned in my answer? thanks.
check to see if you insert to your DB with that extra line.
showing more code would help us understand whats causing your issue...
but if you want to brute force it you could always use substring() for each or your elements or
`String input = EditTextinput.getText().toString();
input = input.replace(" ", "");`
but like #Nitin Sethi said more code especially the entry to your DB would give us a better understanding of what's going on
so what I did is that I've added the word "line space" in my database and I added
in my android code:
String input = //here I take the cde from cursor;
input = input.replace("line space", "\n");
and it worked perfectly but is there another way?

Setting Danish Character Text

I am getting some strings from json. My string contains special characters like "æ" from Næstved an many more like "ø" from køkken. But When I set Text these strings to ant textview, I get my strings printed in unusual way.
Example: For køkken I get k&oslash ;kken.
I think I need to encode or decode my string somewhere but where I don,t know.
Please help.
Thanks in advance
The displayed version of your string represents an HTML encoded entity. You might want to verify that it is not coming in this way in your JSON data, but in any case, to decode it you can use the StringEscapeUtils.unescapeHtml4 method from Apache Commons Lang:
final String escaped = "køkken";
System.out.println(StringEscapeUtils.unescapeHtml4(escaped));
Output:
køkken
Did you check out the Latin Coding for your characters? I know the Ash character can be coded with æ and will show up æ in the browser.
Here is the a list of codes
Hope this helps!

Copy string to strings.xml

Everybody knows that if we have:
ekran3.setText("VAT Tax:");
We may (or even we SHOULD) convert it to:
ekran3.setText(getString(R.string.kwotaVat));
and add in strings.xml:
<string name="kwotaVat">VAT Tax:</string>
But is there some kind of trick to do it automatically? For example by clicking RMB on text and selecting some option? It would be nice to know it in fact it will save us a lot of time than while we're doing it manually.
If you are using Eclipse you may extract the string directly into the strings.xml file by placing the mouse within the string and hitting Ctrl + 1. It will bring up the dialog as followed and you may select "Extract String". You then give it a name (Ex: kwotaVat) and you're done.
hey you do not need to use getString() to convert it to string the values xml file is already having data in string form so you just need to use the following code to set the string
ekran3.setText(R.string.kwotaVat);
where ekran3 is the object of your text view
and kwotaVat is the id of your value string
for more detail od android codes have look here http://grabcodes.blogspot.com/

how to add smilies/emotions in my edittext for a notes app in android?

can any one know about how to add/insert emotions/smiles to text(which ever i typed in my edit text for my notes). i have little confusion about if i want to add these smiles of type .png in to edit text type of string, is it possible? and also i have to save these input into sqlite database. normally i know to store string data taken from edit text.
but along with that text i want to also add smiles symbols where ever my cursor placed and to be store in sqlite data base. and get it back to read.
so guys any ideas, most welcome!
Try to use java (i.e. android) spannable method to implement smiley (i.e.) images for that. You will surely get it, search in google for "how to add images/smiley with java spannable method in android?" you will get good idea.
Reading your question the first thing I can think of is Mapping each image to a sequence of letters, for example :) is smiley.png etc. Now your database also has these smaller representation However while reading from the database you can convert those special character sequences to appropriate image.
For simplicity in seraching those Strings You can wrap them in some less used characters i.e. [ or { or <.

extracting strings from KML file

I am extracting strings from KML file, if the string contains special character like !, #, #, ', " etc. its using codes like '
I am not able to extract entire string if it is like above, by calling getNodeValue(). It is terminating the string at special character.
<name>Continue onto Royal's Market</name>
If i extract the string i am getting only ""Continue onto Royal". I want entire string as
Continue onto Royal's Market.
How to achieve this ?? If anybody familiar with this please reply to this one.
Thanks
Your problem has nothing to do with KML but is general for XML parsning:
Don't use getNodeValue(), as there is no guarantee in DOM that text isn't actually split over several nodes.
Try using getTextContent() instead.
You might also have to replace entities, as in: node.getTextContent().replaceAll("'","'");
In general I wouldnt use DOM at all for extracting data.
I'd use the XmlPullParser as its simpler to work with - and parses faster.

Categories

Resources