Cyrillic symbols in jsonrpc response - java

Using Android Studio and alexd-jsonrpc client I recieve a response, where cyrillic symbols looks like:
{..."ticket_info=ÐÐ¾ÐºÑ 1"...}
instead of:
{..."ticket_info=Мойщик 1"...}
How can i decode this to cyrillic?
JSONRPC request code:
JSONRPCClient client = JSONRPCClient.create(_server, JSONRPCParams.Versions.VERSION_2);
client.setConnectionTimeout(2000);
client.setSoTimeout(2000);
_workplaceList = client.callJSONArray("GetWorkplaceList", companyID);

It looks like an encoding problem. Verify that the service is encoding the JSON-RPC response as UTF-8 and that JSONRPCClient is configured to expect UTF-8.

Related

Gmail API - body of the message contains dash and can't be base64 decoded

I have response from Gmail API encoded in base64.
I used com.google.api.client.util.Base64 to decode this response, and it worked fine.
Now I'm refactoring code and want to update versions of dependencies. I noticed that Base64 class is now deprecated and it's suggested to use com.google.common.io.BaseEncoding. However, when I do the following:
Message asdf = service.users().messages().get(gmailUser, message.getId()).execute();
List<MessagePartHeader> headers = asdf.getPayload().getHeaders();
String data = asdf.getPayload().getBody().getData();
String body = new String(BaseEncoding.base64().decode(data));
I get an error:
com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalArgumentException: com.google.common.io.BaseEncoding$DecodingException: Unrecognized character: -
And indeed, I have some dashes in data that I'm receiving.
Is this a bug in Gmail API? Is there a workaround for that?
Got it!
I shouldn't use BaseEncoding.base64().decode(data), but BaseEncoding.base64Url().decode(data).

How can I set the encoding of a httpExchange response?

I'm trying to modify some server code which uses an httpExchangeobject to handle the server's response to the client.
My issue is that for responses containing characters not supported by iso-8859-1, such as Chinese characters, I get something like '????' in place of the characters. I'd like to set the encoding of the response to utf-8, but have thus far been unsuccessful in doing so.
I tried adding this line:
httpExchange.getResponseHeaders().put("charset", Arrays.asList("UTF-8"));
This successfully puts a "charset" header in the response, but I still can't send the characters I want in the response.
How do I set the encoding of the response to allow for these characters?
Use Content-Type header to specify encoding.
String encoding = "UTF-8";
httpExchange.getResponseHeaders().set("Content-Type", "text/html; charset=" + encoding);
Writer out = new OutputStreamWriter(httpExchange.getResponseBody(), encoding));
out.write(something);

Parsing and encoding URL before sending to Rest webservice

I am developing an App where I am sending get request to REST web service using
#RequestMapping(value = "myURLpattern", method = RequestMethod.GET, produces = "image/png")
I came across a bug if somebody enters # in the URL query String it breaks from there
http://myserver.com:8080/csg?x=1&y=2#4&z=32&p=1
So I tried to encode it via filter HTTPRequestWrapper so that # will be replaced by %23 using URLEncoder.
My problem is for encoding URL, first I need to read the URL(request.getRequestURL()).
and getURL again can't read string after #.
request.getRequestURL() will return only (http://myserver.com:8080/csg?x=1&y=2)
Is there any other way to parse the complete url and encode it before sending to REST web service?
That is not a bug. Check this:
What is the meaning of # in URL and how can i use that?
A # in the url query string is wrong. You should encode it on client side, before sending it to your server. See this one: How to pass '#' in query string
It is in asp, but you should find the java equivalent.
Something to get you started can be this one Java URL encoding of query string parameters

Java + Jersey- sending utf-8 encoded data

In my application I need to use the rest api of a web service. For now I need to send an xml message. The problem is, some of the characters in this xml are polish diacritics. Now, the code of my message sending looks like this
WebResource r = client.resource(resourceAddress);
String response = r.accept(
MediaType.APPLICATION_XML_TYPE,
MediaType.APPLICATION_JSON_TYPE,
MediaType.TEXT_HTML_TYPE
)
.type(MediaType.TEXT_XML_TYPE)
.header("Authorization", authorizationString)
.post(String.class, event);
Java Strings are UTF-16 and my XML should be UTF-8 encoded. Is there a way to tell Jersey to change somehow the encoding before serialization? Or maybe there is some other way, so I can send this String data as UTF-8 and not UTF-16 using Jersey client api?

Sending UTF-8 values in HTTP headers results in Mojibake

i want to send arabic data from servlet using HTTPServletResponse to client
i am trying this
response.setCharacterEncoding("UTF-8");
response.setHeader("Info", arabicWord);
and i receive the word like this
String arabicWord = response.getHeader("Info");
in client(receiving) also tried this
byte[]d = response.getHeader("Info").getBytes("UTF-8");
arabicWord = new String(d);
but seems like there is no unicode because i receive strange english words,so please how can i send and receive arabic utf8 words?
HTTP headers doesn't support UTF-8. They officially support ISO-8859-1 only. See also RFC 2616 section 2:
Words of *TEXT MAY contain characters from character sets other than ISO- 8859-1 [22] only when encoded according to the rules of RFC 2047 [14].
Your best bet is to URL-encode and decode them.
response.setHeader("Info", URLEncoder.encode(arabicWord, "UTF-8"));
and
String arabicWord = URLDecoder.decode(response.getHeader("Info"), "UTF-8");
URL-encoding will transform them into %nn format which is perfectly valid ISO-8859-1. Note that the data sent in the headers may have size limitations. Rather send it in the response body instead, in plain text, JSON, CSV or XML format. Using custom HTTP headers this way is namely a design smell.
I don't know where word variable is comming from, but try this:
arabicWord = new String(d, "UTF-8");
UPDATE: Looks like the problem is with UTF-8 encoded data in HTTP headers, see: HTTP headers encoding/decoding in Java for detailed discussion.

Categories

Resources