Still having problem with imported text from my database to Android - android

I have had a problem with formattings symbols shown on the image. I got help from this site and I used Html.fromHtml() and hope that it would use the formatting, the only thing it did was to remove the formatting symbol but not anything else like newline or so.
If I cant find a way to use the formatting I wonder if it is possible to add a "\n" everytime the symbol is shown. The thing is if I try to use a method like
(Ps the "[]" is used here instead of the real symbol cause I cant find out how to write it.
int nr=theTextString.indexOf("[]")
and then replace the text with
theTextString=theTextString.substring(0,nr)+ "\n" + theTextString.substring(nr);
but the problem is that the symbol is not represented as a character to look for the index at, not in any way I know of. Would really be thankful for help.

If [] crap character is only shown on the last chacter do this
Log.i("My String before:", theTextString);
theTextString = theTextString.substring(0, theTextString.length()-2);
Log.i("My String after:", theTextString);
Btw: [] <-- is not the same character (Infact, there are two characters) it is something else.
Solution: To encode the string
theTextString = URLDecoder.decode(theTextString, HTTP.UTF-8);

Related

URL encoding is getting failed for special character. #Android

I'm working on a solution where need to encode string into utf-8 format, this string nothing but device name that I'm reading using BluetoothAdapter.getDefaultAdapter().name.
For one of sampple I got a string like ABC-& and encoding this returned ABC-%EF%BC%86 instead of ABC-%26. It was weird until further debugging which helped to identify that there is difference between & and &. Second one is some other character which is failing to encoded as expected.
& and & both are different.
For encoding tried both URLEncoder.encode(input, "utf-8") and Uri.encode(input, "utf-8") but nothing worked.
This is just an example, there might be other character which may look like same as actual character but failed to encode. Now question are:
Why this difference, after all it is reading of some data from device using standard SDK API.
How can fix this be fixed. Find and replace with actual character could be a approach but scope is limited, there might be other unknown character.
Any suggestion around !!
One solution would be to define your allowed character scope. Then either replace or remove the characters that fall outside of this scope.
Given the following regex:
[a-zA-Z0-9 -+&#]
You could then either do:
input.replaceAll("[a-zA-Z0-9 -+&#]", "_");
...or if you don't care about possibly empty results:
input.replaceAll("[a-zA-Z0-9 -+&#]", "");
The first approach would give you a length-consistent representation of the original Bluetooth device name.
Either way, this approach has worked wonders for me and my colleagues. Hope this could be of any help 😊.

How to return apostrophe when using Google Translate API for Android?

I have an Android application that uses Google Translate API.
Everything works great, including when I tried to translate phrases that include apostrophe like "We've eaten" to Spanish.
However, problems occur when the translation result I should be getting back contains an apostrophe. For example, when I translate a Spanish phrase, "A ver", into English, it returns "Let&#39s see" with a ";" after "9". It seems like whenever I have a phrase that should return an apostrophe, it returns "&#39" with a ";" after "9". (Not placing ";" after "9" because it gets converted to an apostrophe by stackoverflow).
I can think of a way to solve it. After I get the translation result, I can match the string for ""&#39" + ";" and replace it with an apostrophe.
However, I don't feel like this is the way I should approach it. It's very unlikely that a user will actually type in "&#39" as an input for translation, but hard coding a manual conversion like this seems like it might cause problems down the road. I'll love to hear your thoughts on this.
Please let me know how I should fix/approach this issue.
Thank you!
The best solution is to add &format=text to your query.
You are correct hard codding is not solution,
But you can convert this HTML entity back to apostrophe, by Using HTML classes provided already.
Html.fromHtml((String) "Let's see").toString()
Above code will convert any valid HTML entity.
I Hope this is what you are looking for.
Thanks Guillaume. For those using php.
$translation = $translate->translate($stringToTranslate, ['target' => $target, 'format' => 'text']);
Thanks Guillaume. For those using go. (api v3)
req := &translatepb.TranslateTextRequest{
MimeType: "text/plain", // add this line to request
}

Adding a href tag to a long string from a JSON

I've been stuck on a problem where I have a json object with a value in it titled message, which will have lots of string and some will even contain links in the format below (like how this site has different formats for their bold, italics, etc)
[[www.randomwebsite.com]] gives www.randomwebsite.com
or
[[www.randomwebsite.com random]] gives random
The regex that I have for the top one is:
\[\[.[^\]]*\]\]
and I'm doing a .replaceAll to try and add the href tags to it, but it isn't working as I feel I've done it wrong. My code for that is
String htmlHyperlinkMessage = htmlStrikeMessage.replaceAll(Constants.HYPERLINK_REGEX, "$1");
but this gives me an array out of bounds exception and I'm just stuck on this, can anyone give any suggestions?
It's greatly appreciated, I have looked around on the forum but I couldn't find anything related to my example as the format for different hyperlinks has confused me.
EDIT
Here's my stacktrace logcat
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
at java.util.regex.Matcher.group(Matcher.java:579)
at java.util.regex.Matcher.appendEvaluated(Matcher.java:138)
at java.util.regex.Matcher.appendReplacement(Matcher.java:111)
at java.util.regex.Matcher.replaceAll(Matcher.java:319)
at java.lang.String.replaceAll(String.java:1600)
at com.myapp.android.model.PostItem.getMessage(PostItem.java:98)
When you're replacing something with a group (like $1, $2), you need to capture that group using parentheses. You're getting an out-of-bounds exception because $1 tries to find a group you made, but your regex \[\[.[^\]]*\]\] contains no groups.
If you want to capture the contents of [[]] as a group, you can add parentheses: \[\[(.[^\]]*)\]\]
For a further explanation, read the "3.4. Grouping and back reference" part of this article: http://www.vogella.com/tutorials/JavaRegularExpressions/article.html
And the replacement "$1" doesn't seem right either. It should be something like "$1"
By the way, I don't think the . character in your regex is needed. It does make sure the square brackets aren't empty, but you can achieve the same effect more clearly by replacing the * with + like this:
\[\[([^\]]+)\]\]

\n not breaking lines in Java

I use a google api to generate a QR code from some data. It should be represent a VCARD format.
I call this url.
When i read the QR code, i nicely got back all the information i added to the link, except one little error.
The line sperators not working.
I got back this in Java (Android):
BEGIN:VCARD\nVERSION:2.1\nFN:Adam Varhegyi\nN:Adam;Varhegyi\nEMAIL:somemai#address.com\nTEL:1234567\nINTERNET:;\n\nORG:Mycompanyname\nEND:VCARD
Instead of this: (\n = linebreaks)
BEGIN:VCARD\nVERSION:2.1
FN:Adam Varhegyi
N:Adam;Varhegyi
EMAIL:somemai#address.com
TEL:1234567
INTERNET:;
ORG:Mycompanyname
END:VCARD
I tryed to work it arround with using a Scanner like this:
Scanner sc = new Scanner(myVCardStringInputFromQrCode);
sc.useDelimiter("\n");
while(sc.hasNext()){
String str = sc.next();
Log.i("VCARD LINE: ", str);
}
And this method only gives back 1 line! It is also ignores the "\n" marks.
Edit:
I also tried to use System.getProperty("line.separator") , but no use.
Edit part 2:
if(myVCardStringInputFromQrCode.contains("\n")){
Log.i("Found linebreak", "TRUE");
}
else{
Log.i("Found linebreak", "FALSE");
}
This code gives me back "FALSE" - Java says it is not contains "\n" when i clearly see it is.
Anybody know whats happening here?
Edit part 3:
The correct answer was deleted for some reason so i cannot mark it as "answer".
The solution was "\\n" instead of "\n" and it is working.
you can use System.getProperty("line.separator")
The \n you are seeing is not an actual line break. It is an escaped line break (a backslash, followed by an "n" character).
Try replacing all occurrences of this with an actual line break. Note that you should use the \r\n newline sequence because this is the newline sequence that vCards are supposed to use according to the specs.
myVCardStringInputFromQrCode = myVCardStringInputFromQrCode.replace("\\n", "\r\n");
Remember to pass \\n into the first argument and not \n. You need two backslashes in order to get a literal backslash.
\r\n instead of \n always worked for me.

Want to get the Special Character Parsed in the Response

I am getting the Special Character " ã " and a dash sign " - " in my Text Response originally and When I perform the setText() on that text it shows me " � " sign at both the Places in my Device and emulator.
I want to show what exactly I have in the response, I have no clue what I do to look like what it actually is. I have googled much and tried various stuff like UTF-8, ISO Encoding Standards and HTML.fromHTML and others, but all useless.
Does anyone have the answer for this.
Thanks
DavidBrown
You can save the character as an Object and then retrieve the object and set it in the textview.
you can replace these special character with their ascii code either at server side or at client side. see if this works

Categories

Resources