All I want to do is to send a URL String into my RESTFUL web service with some kind of code like this
URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText());
Its all going well until I found error with space character in my URL. I found some solution about replacing the space character with %20 which is I already defined with something like this :
URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText().replace(" ", "%20"));
Everything, again, seems going well until i found that the replace(Char oldChara, Char newChara) function can only replace ONE space character, and not two.
For brief example when I send the CharSequence.getText() with values "We won" there will be no error, but when I change the values into "We won the battles" there will be an error issuing that there are some illegal character sent to my RESTFUL web service.
Any kind of answer will come up with my great thanks and big salute
~Regards~
Use replaceAll instead of replace.
Although, you should really be doing proper URL encoding. You can use URLEncoder.encode
for example.
Related
I send an SMS saying "Click on https://www.example.com/?"
When I receive this SMS on an Android phone, only the https://www.example.com is clickable. The "?" is visible in the SMS, but is not part of the clickable URL.
Is there some way to escape the ? or do some other workaround so that click on the link goes to https://www.example.com/?
EDIT: Forget Android - even on this page on stackoverflow - the "?" is not part of the clickable link
Can you replace the question mark (?) by %3F
www.example.com/%3F
See:
https://www.w3schools.com/tags/ref_urlencode.asp
This appears to be correct behaviour (as far as I have observed on other systems) - an empty query string (effectively a dangling question mark) is not necessarily part of the URL so appears as punctuation.
As a workaround my suggestion is to ensure you always provide a parameter:
https://www.example.com/?a=a
Ideally you can find something that makes the link enticing to the user (rather than only confusing):
https://www.example.com/?vip=yes
Because these are not query parameters you are actually using, they should not affect your page processing
Hi you can send by converting to short url by https://goo.gl/ and you can get same as expected .
My Android app is making two GET calls to my Server API. In the first one, is this, where parameter code is a 256 char String.
$.getJSON( myServerEndpoint, {
action: "doStuff1",
username: $("#username").val(),
code: my256charString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
The second one is this, where parameter code is a 5120 char String. Both reach the same server endpoint.
$.getJSON( myServerEndpoint, {
action: "doStuff2",
username: $("#username").val(),
code: my5120CharString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
When I call both of them from the same device and same user connected to WiFi or most mobile data providers, it works perfectly.
However, when I connect from a Vodafone data connection, the second request never reaches the server. I cannot find any other explanation than that there is a limit on the length of the parameters with Vodafone.
Any ideas or solutions?
OK, so here it goes. First, read this: What is the maximum length of a URL in different browsers?
Yes, there's a limit in the length of the "URL", but someway I don't know how to explain why it is happening only for vodafone. Plus, I don't even know how the request pass through their servers anyways.
As for the solution, you should consider changing from GET request to POST request when the payload is too big.
Quick solution: Base64-encode the code part of the message. Downside: you must decode on the server. This is a standard function in most languages though.
If you're already using Base64 or somesuch cypher, what about Blobs? https://developer.mozilla.org/en-US/docs/Web/API/Blob
chromano's suggestion is spot-on too, just switch to POST and you will definitely get an unlimited Post Body. Downside: Have to JSON.stringify and JSON.parse for yourself, and if you want to expose this URL to a user (say as a link to share) it now can't carry the same information (URL's are GET requests).
I am trying to send data using the following url but Its somehow being declared invalid query in Logcat
Base_URI/users/add.json?json={“email”: xxx#x.com, “password”: “xxxxxxxxx”, “first_name”: “Xyz”, “last_name”: “Xyz”}
I have asked this question before but found no solution so I am being more specific now.
The web services are made in cakephp as far as I know. Its not accepting url because of this part "json={}". Also the parameters are separated by ',' rather than '&'. In short, what encoding should I use to get json from it ?
I get this error in Logcat:
Illegal character in query at index 56
one more thing, No suggestions for third party library unless there is no solution in org.apache.http . By the way I tried Volley Library but had no success.
Finally after debugging and trying different solutions the whole day, I solved my own problem :)
I needed to encode the parameters part and not the whole URL like this:
String url = "Base_URI/users/add.json?json=";
url =url + URLEncoder.encode("{\"email\":\""+email+"\",\"password\":\""+password+"\"}", "UTF-8");
Thanks everyone for taking part in it !
Try this:
Base_URI/users/add.json?json={"email": "xxx#x.com", "password": "xxxxxxxxx","first_name": "Xyz", "last_name": "Xyz"}
Hi Everyone,
I am fetching a text from my DB and before inserting the text into db i know that the encoding of text is ISO-8859-1 , but after fetching from db and before loading this text i am checking the encoding through this code
InputStreamReader is = new InputStreamReader(new ByteArrayInputStream(body.getBytes()));
is.getEncoding();
Log.v("encoding", ""+is.getEncoding());
// String body = fetched from db
and i am getting in the log for the encoding of the text is UTF-8. And this text is not getting loaded on the webview with this method :
mailView.loadDataWithBaseURL(null, body, "text/html", "UTF-8", null);
please suggest me a correct way to solve this problem.
This reply is terribly late, but I stumbled on the question via Google and so thought I'd answer.
As described in the JavaDoc, new InputStreamReader(InputStream) will create a reader with the system default (apparently UTF-8). is.getEncoding() is simply returning that default which may or may not match your stored data.
In general, it is a good idea to specify the encoding of your stream explicitly. The implication is that you need to store the encoding along with the content. You can use out of band knowledge (e.g., my application only uses ISO-8859-1) but this will be brittle in the event that you change your chosen encoding in the future.
Since the world isn't always a nice place, and strings get separated from their charsets, you might look into a charset detector. See http://userguide.icu-project.org/conversion/detection as an example.
This is my JSON code. Note the title field is base64_encoded. I want to get this title field value.
{"item":{"id":"1","title":"ZGVtbyBkZXNjcmlwdGlvbiBkZW1vIGRlc2NyaXB0aW9uIA==","status":"1"}},
{"item":{"id":"4","title":"ZGVtbyBldmVudCBmb3IgZGVtbyBkZXNjcmlwdGlvbg==","status":"1"}}
{"item":{"id":"6","title":"ZGVtbyBkZXNjcmlwdGlvbiBkZW1vIGRlc2NyaXB0aW9uIA==","status":"1"}}
The title text is in base64 format (ZGVtbyBkZXNjcmlwdGlvbiBkZW1vIGRlc2NyaXB0aW9uIA==) but I would like to get it as normal text -- how can I do this easily?
Thanks.
unfortunately the entire point of encryption is to stop people from doing this...
If you are legally obtaining the encrypted data, try getting in touch with the server team or checking their documentation to find out how they encrypt and how you should decrypt.
There isn't a one size fits all solution for this problem. The answer is 100% dependent on how the String is encrypted in the first place.
good luck!