what is the equivalent of "-" in an url? - android

How to change - in url.
I have got the same problem with space so i changed it with %20 and it worked:
name = name.replaceAll(" ","%20");
What is the equivalent of "-" in an url?
I tried %2C and it doesn't word: %2C -> ","

Use URLEncoder.encode(String s) to encode all your strings that you are gonna use for urls.

As far as I know the dash char is considered a safe char in the URL. If you have problems, then the problems might have nothing to do with the dash. Try, though, to replace it with %2D.

It's the ascii code (translator). - translates to %2d. You were close :)

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
}

Trouble Removing Whitespace in URL (Android Studio)

So I have a for loop that retrieves a room name to add to my baseurl, and so my URL will be this:
a = (baseUrl + roomName + "/users");
It works for all the rooms except one, which has a space, so I tried using this:
a.replaceAll("\\s+", "");
and other attemps but they return this
System.err﹕ java.io.FileNotFoundException: https://example.com:8443/conferenceRoom/First Floor/users
System.err﹕ at http.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
Basically, I have a link like https://example.com:8443/conferenceRoom/First Floor/users
and I want it to be https://example.com:8443/conferenceRoom/FirstFloor/users so no space between 'first' and 'floor'
(note I do use openURLConnection(a) ) when actually using the URL later but I don't think it has anything to pertain to my problem, just wanted to clarify that a is a string
If you want to remove whitespaces from your url you can use trim() as
a= a.trim();
If you want to replace whitespaces with "%20" then you can use
a = a.replace(" ", "%20");
Having spaces in URLs can cause problems in certain situations. Therefore, a lot of the time spaces are replaced with %20 (which is the ascii encoded value for a space in a URL string). If you remove spaces from your URLs, you won't have the %20s. Otherwise, they're going to stick around.
Your url might contain UTF whitespaces which are not covered by the \s mofidier.
Just use trim which removes all whitespaces:
a = a.trim();

Equals sign in url not working

i got a problem which i tried hard to solve with searching with google because will be a simple solution.
I want to open the following url:
http://<<IP>>/query.html?sql="select * from ADAnreden"
To do this, i write this url in a string to open it with HttpGet...
String url = "http://"+ip+"/query.html?sql=\"select * from ADAnreden\"";
So i escaped the " infront of select and after ADAnreden. But the problem is that the following error is comming up:
Illegal character in query at index 36.
This is the equals sign. So how can i escape the = ? The backslash is not working.
Thanks for help
The issue is the escaping for the URL, not for Java. Spaces are not valid in URLs. See this answer for more about URL encoding in Android.
you must encode the query before using it as URL, see URLEncoder.encode(query);

\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.

Categories

Resources