IllegalArgumentException IN ANDROID URL - android

Using the below url I got an error:
java.lang.IllegalArgumentException: Illegal character in path at index 47: http://safetracker-threetinker.rhcloud.com/api/{userid}/locations?lat={latitude}&lng={longitude}.
URL:
URL=http://safetracker-threetinker.rhcloud.com/api/{userid}/locations?lat={latitude}&lng={longitude}
how to solve the error. I don't have good knowledge in URL encoding. please help me to find the solution.

The problem is actually it is looking for long/integer value and you are passing a { just put a $ so that it will be replaced by the actual value pointed by the variable
http://safetracker-threetinker.rhcloud.com/api/1/locations?lat=5&lng=5
your Address is like this
URL=http://safetracker-threetinker.rhcloud.com/api/{userid}/locations?lat={latitude}&lng={longitude}
it should be like this
URL=http://safetracker-threetinker.rhcloud.com/api/${userid}/locations?lat=${latitude}&lng={longitude}

We can not use some special characters in URL, so we have to replace these special characters with its encoded form.
Replace your URL with following URL
URL=http://safetracker-threetinker.rhcloud.com/api/%7Buserid%7D/locations?lat=%7Blatitude%7D&lng=%7Blongitude%7D
May this help you.

URLEncoder should be the way to go. You only need to keep in mind to encode only the individual query string parameter name and/or value, not the entire URL, for sure not the query string parameter separator character & nor the parameter name-value separator character =.
String q = "replace_with user_id/locations?lat=replace with latitude&lng=replace with longitude";
String url = "http://safetracker-threetinker.rhcloud.com/api/=" + URLEncoder.encode(q, "UTF-8");

Related

Convert string to a valid URL link

I am parsing some values from a website with JSoup, some of them are also url links (href).
When I get the url link, which I set to a string. The string sometimes does not become a valid URL link as it has a special character like '!?()
Example: https://somelink.com/King's+Beak (The ' makes the link not valid).
Now I tackle this by replacing the characters with the default character set for UTF-8, which works as it should.
Example code:
String test = arTD.select("a.wiki_link").get(0).attr("href").replaceAll("'", "%27");
I also set JSoup to UTF-8 but that does not seem to work.
Document document = Jsoup.parse(response.body().string(), "UTF-8");
Now my question is, is there a more convenient way to tackle this?, as I need to escape more characters like '!?().
Thank you in advance.
One way to solve this issue is to use the URLEncoder.encode() method to encode the URL string. This method will replace special characters with their corresponding ASCII codes.
String test = arTD.select("a.wiki_link").get(0).attr("href");
String encodedUrl = URLEncoder.encode(test, StandardCharsets.UTF_8);
Another way to solve this issue is by using the Uri.Builder class in android to encode the URL.
Uri.Builder builder = new Uri.Builder()
.scheme("https")
.authority("link.com")
.appendPath("King's+Beak");
Uri uri = builder.build();
String encodedUrl = uri.toString();
The first method will encode the whole url while the second method will only encode the path of the url.
Choose the suitable one for you.

How can i pass an argument as string with line break \n in EvaluateJavascript?

I'm having an issue when using the EvaluteJavascript on both Android and iOS (I'm using Xamarin).
The issue is when I want to pass a string argument to a javascript function, if that string contains special character, the js compiler will not understand.
For example:
EvaluateJavascript("updateHtml('Some Html \n Some Html')")
But if i use, this will work:
EvaluateJavascript("updateHtml('Some Html Some Html')")
So the question is how am i able to pass entire string as argument to the javascript function in EvaluateJavascript.
Thanks in advance :)
I was able to solve my issue.
I encoded my string as URL encoding like this
EvaluateJavascript('$"updateHtml({Uri.EscapeUriString("Some Html \n Some Html"'})))
Then in my javascript i just need to decode it by using:
document.getElementById("body").innerHTML = decodeURI(html);
How about using "\\n" instead of "\n" ?

How to avoid nul value in URL in android?

I have a strange issue about using Retrofit2 in my android project. I got the issue about the server error since the request is something like that.
https://www.example.com/api/v1/skills?q=Good%00
Since the invalid value "%00" is not acceptable in our server, so it showed error on my activity.
API service
#GET("skills")
Observable<SearchItem> getSkills(#Query("q") String keyword);
In my fragment, I just get the text using following simple statement.
String keyword = editText.getText().toString()
api.getSkills(keyword);
What I want to know is the following:
Is it possible to have a word can be converted to "%00" ?
How to avoid this "Good%00" before I send to getSkills function?
To enable compile time checks on nullity add #NonNull annotation,
#GET("skills")
Observable<SearchItem> getSkills(#NonNull #Query("q") String keyword);
Another way is to change each "%00" in your string, using .replace()
Replace the string with "" if it contains %00
if (text.toString().contains("%00")){
text = text.replace("%00", "");
}
and then call getSkills(text) with updated value
Try this
Use trim()
The java string trim() method eliminates leading and trailing spaces. The unicode value of space character is '\u0020'. The trim() method in java string checks this unicode value before and after the string, if it exists then removes the spaces and returns the omitted string.
SAMPLE CODE
String keyword = editText.getText().toString().trim();
api.getSkills(keyword)
or your can use
replace()
The java string replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence.
SAMPLE CODE
url =url.replace("%00", "");
or You can use URLEncoder
Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding
String encodedurl = URLEncoder.encode(yourURL,"UTF-8");
There is two way to solve this
1. you restrict the entry write below code
android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
you can add more thing which you want
String keyword = editText.getText().toString().replace("%00", "");
api.getSkills(keyword);

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);

Illeagal character in HTTPPost Method Android

AM getting continuously the illegal character error in this url :
http://my.server.com/SelectPerson-0.1/nativeService/businessCardSave?company=Select Person&fullName=Rghh Dgivv&email=uday#uioperr.info&phoneNo=123456789&desiredPosition=Machine Operator&city=Dthhbv&state=AZ&zipcode=5369466&currentEmployer=Fgyevb&currentJobTitle=Dyhg &careerArea=Health and Personal Care&datasource=Android&location=Dthhbv AZ&did=J8C4N275YH1N9LGSZCG
Previously there was an extra & symbol , so I removed but still am getting same error.
Can anyone help me, in finding this illegal character in this url.
or anyone specify , the link for Httpost illegal characters
Thanks
# should be encoded (see the full list of characters that need encoding). For simplicity, you should just use URLEncoder.encode(yourString, "UTF-8"); for each of the values you're putting in the query string of that URL.
You probably need to replace the spaces in the url with %20

Categories

Resources