Hey, how can I read a value of a cookie?
Example:
String cs = CookieManager.getInstance().getCookie();
System.out.println("Cookies string: "+cs);
This will give me a string which has to be parsed with split on ';' and '='. Is there a "cookie string reader" or smth? Is there any other way of reading the value of only one particular cookie in the webview?
Thx!
Well, I suggest that you parse the string into an Array yourself. That would then be something along these lines in standard Java:
String[] x = Pattern.compile(";").split(CookieManager.getInstance().getCookie());
Now you have an Array of name - value pairs, which you can further parse and then store.
Related
My problem is that I am getting strings where some characters are Unicode.
"fieldName": "Ac6jHguQjKKUxx6MSOpjO2kOLKPAdjStVs1pgTGNSU8\u003d"
Then I immediately send such a string to another API and the server returns me an error with a code of 500. If I use this string in postman and replace the unicode with a normal one, then the code 200 is returned from the server.
I thought there was a problem in the server, but they checked it and said that they were sending it as expected.
How do I translate Unicode?
The easiest way is to use URLDecoder. Here is an example.
String str = "Ac6jHguQjKKUxx6MSOpjO2kOLKPAdjStVs1pgTGNSU8\u003d";
String decode = URLDecoder.decode(str, "UTF-8");
System.out.println(decode);
//Ac6jHguQjKKUxx6MSOpjO2kOLKPAdjStVs1pgTGNSU8=
my json is
{[{"key1":"value1","key2":"valu2"},{“ key3":"value3","key4":"valu4”}]}
How to change the above text as follows. Thank you for helping my friends
{"travel": [{"key1":"value1","key2":"valu2"},{ key3":"value3","key4":"value4}]}
Your original String is not a valid JSON for two reasons:
1 we can see some invalid quotes
2 elements inside a json object must have keys.
so assuming that your String is correct to achieve what you need in java you can do the following:
String string="{[{\"key1\":\"value1\",\"key2\":\"valu2\"},{\" key3\":\"value3\",\"key4\":\"valu4\"}]}";
StringBuilder stringBuilder=new StringBuilder(string);
stringBuilder.insert(1,"\"travel\":");
String json=stringBuilder.toString();
You just insert your key after the first character. Hope this will help.
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");
I am trying to parse the "html_instructions" string from the "steps" array at this link:
I have no trouble parsing the string, but it returns with bits of code mixed in. For Example, the parsed string of:
"html_instructions" : "Head \u003cb\u003esouthwest\u003c/b\u003e toward \u003cb\u003eCapitol Square SW\u003c/b\u003e",
Appears as:
Head<br>southwest"</br>"towards<br>...
Instead of appearing simply as:
Head southwest towards...
Is there a way i can format the string to remove the "breaks"? Any help is greatly appreciated.
You can use a regex to remove HTML tags from your content.
String htmlString = "Head<br>southwest</br>towards<br>...";
String noHtml = htmlString.replaceAll("<[^>]*>", "");
Look into answer for this question.
How to convert unicode in JavaScript?
Basically the response is in Unicode, hence the < is represented by \u003c & > by \u003e you can use String manipulation to replace these strings with appropriate charterers.
Try doing something like this
String parseResponse = response.replaceAll("\u003c","<");
This will get your string in the proper html format.
i have a String displayed on a WebView as "Siwy & Para Wino"
i fetch it from url , i got a string "Siwy%2B%2526%2BPara%2BWino". // be corrected
now i'm trying to use URLDecoder to solve this problem :
String decoded_result = URLDecoder.decode(url); // the url is "Siwy+%26+Para+Wino"
then i print it out , i still saw "Siwy+%26+Para+Wino"
Could anyone tell me why?
From the documentation (of URLDecoder):
This class is used to decode a string which is encoded in the application/x-www-form-urlencoded MIME content type.
We can look at the specification to see what a form-urlencoded MIME type is:
The form field names and values are escaped: space characters are replaced by '+', and then reserved characters are escaped as per [URL]; that is, non-alphanumeric characters are replaced by '%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks, as in multi-line text field values, are represented as CR LF pairs, i.e. '%0D%0A'.
Since the specification calls for a percent sign followed by two hexadecimal digits for the ASCII code, the first time you call the decode(String s) method, it converts those into single characters, leaving the two additional characters 26 intact. The value %25 translates to % so the result after the first decoding is %26. Running decode one more time simply translates %26 back into &.
String decoded_result = URLDecoder.decode(URLDecoder.decode(url));
You can also use the Uri class if you have UTF-8-encoded strings:
Decodes '%'-escaped octets in the given string using the UTF-8 scheme.
Then use:
String decoded_result = Uri.decode(Uri.decode(url));
thanks for all answers , i solved it finally......
solution:
after i used URLDecoder.decode twice (oh my god) , i got what i want.
String temp = URLDecoder.decode( url); // url = "Siwy%2B%2526%2BPara%2BWino"
String result = URLDecoder.decode( temp ); // temp = "Siwy+%26+Para+Wino"
// result = "Swy & Para Wino". !!! oh good job.
but i still don't know why.. could someone tell me?