Why java string display \u on screen - java

Suppose \u4404 means 'A' in Japanese.
Following code will print 'A' on screen.
String str = "\u4404";
System.out.println(str);
This is true because encode("\u4404", unicode)='A'.
But I encountered one problem. When I process a message received over http, I get following output like
{"name":"\u4404\u2424\u4022","age":"30"}
The http header shows the reply is encoded by utf-8. But why does the ouput shows like this?
Here is my guess:
Suppose the stream I received from web is mystream. Then, after we encode mystream with utf-8, I get \u4404\u2424\u4022. I have to encode mystream by TWO times, and get the right 'A' in Japanes.
Am I right? If i am right, why transfer data like this? Because of JSON? Thans very much for your ansewer!

Related

HTML entities incorrectly encoding in API call

I am about to use Paxful API method for sending a message to trade:
String message = "Do NOT PRESS 'Paid' button, until your transfer get 'Success' status.";
paxfulService.sendMessage(tradeId, message);
But here is what I see in the browser:
This is my fault, or Paxful API use unnecessary HTML encoding?
The API you are using is re-encoding the single quotes back into their hex values.
In your original message string try using ' in the place of the single quotes you have.

Unicode char appears as ?? on debian

I'm parsing the JSON received from a Minecraft server's ping request. The code works fine on Windows and gives the following output:
§4§l> §f§l> §4§l> §7-=[ §5§lMythCraft §6§lNetwork §7]=- §4§l> §f§l> §4§l> §7-=[ §b§lFaction 1 Has Reset §e➸ §c§lFresh Map! §7]=-
However, on my Debian VPS the following is outputted instead:
??4??l> ??f??l> ??4??l> ??7-=[ ??5??lMythCraft ??6??lNetwork ??7]=- ??4??l> ??f??l> ??4??l> ??7-=[ ??b??lFaction 1 Has Reset ??e??? ??c??lFresh Map! ??7]=-
I would assume that this is an encoding issue. Am I correct? How can I fix it?
The ping code is here.
I figured it out.
The default charmap on my machine wasn't UTF-8, so it couldn't process the characters correctly, and replaced with with a ? instead.
To fix it, I changed the definition of the String json to be this:
String json = new String(in, Charset.forName("UTF-8"));
That way the returned String is processed in UTF-8 instead of the default encoding.

Why I am not able send header with /(forward slash) like character?

I am getting cookie value in jstring. Server is sending it as base64encoded UTF8 string. I compared string from server and my end, and I am getting exactly same string.
Now I need to decorate this value with n= as prefix and ; as suffix. (Which I am doing in line no. 2 of code).
If I do not use line no. 1, string goes null to Java Server. Otherwise server is getting value.
jstring = [jstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *cookieVal=[NSString stringWithFormat:#"n=%#%#",jstring,#";"];
[self.requestSerializer setValue:cookieVal forHTTPHeaderField:#"Cookie"];
We are using AFNetworking in iOS for request and response. We have observed very strange pattern,
If string contains /(forward slash) then we are getting padding error on Java server, if string doesn't contain /, then string will go as required.
As you can see in line no. 3, we are sending this value as header of http/https request.
I have tried many things, like this (tried very last code with my string.). Also, tried to use different encoding, but problem still persists.
This url conversion would not convert all the special characters we have in ios device keypad.
we have to convert this with blow function. use this as category.
- (NSString *) URLEncodedString_ch {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)#"!*'\"();:#&=+$,/?%#[]%~_. ", CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
}

Java > PHP Socket - trash at start of message

I have a java server communicating with a PHP script called from apache. I am aiming to send a JSON from the java server to the php client when requested, however there is some stuff getting prefixed when its received on the client.
JAVA
in = new BufferedReader(new InputStreamReader (socket.getInputStream()));
out= new DataOutputStream(socket.getOutputStream());
//The server receives a JSON from the PHP script and replies. It recives and converts to a Gson JSON no problem.
String reply = "{\"status\":\"reg\",\"token\":\""+client.getToken()+"\"}\r\n";
//reply = "HELLO\r";
out.writeUTF(reply);
PHP
$rec = socket_read($socket, 2048,PHP_NORMAL_READ);
echo "Receiving... ";
echo $rec;
The issue is that the message received is pre-fixed with some crap.
Output From PHP
Receiving... 1{"status":"reg","token":"QOPIPCNDI4K97QP0NAQF"}
If I send "HELLO\r"
Receiving... >HELLO
You shouldn't use DataOutputStream.writeUTF() unless you are using DataOutputStream.readUTF() to read the message.
Here is a snippet of the javadoc of writeUTF():
Writes a string to the underlying output stream using modified UTF-8
encoding in a machine-independent manner.
First, two bytes are written to the output stream as if by the
writeShort method giving the number of bytes to follow. This value is
the number of bytes actually written out, not the length of the
string. Following the length, each character of the string is output,
in sequence, using the modified UTF-8 encoding for the character. If
no exception is thrown, the counter written is incremented by the
total number of bytes written to the output stream. This will be at
least two plus the length of str, and at most two plus thrice the
length of str.
The bolded part above may tell you why you are getting weird characters at the beginning of your message.
Here is a workaround I believe will work in your case
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
out.write(os.getBytes("UTF-8"));
Reference: Why does DataOutputStream.writeUTF() add additional 2 bytes at the beginning?

IOS Receipt validation IllegalArgumentException

I use similar code as its shown here in the question.
Java and AppStore receipt verification
But I still end up getting
{"status":21002, "exception":"java.lang.IllegalArgumentException"}
Can it be a problem at Base64 encoding?. Do I have to convert the base64 encoded string into hex or something else?.
What i post is similar to following
{"receipt-data" : "eyJzaWduYXR1cmUiOiJBbjNJVER0VVNmZWNhaGMxR.....
The problem was at Base64 encoding inside Java. When I do the encoding inside IOS and use that as the request from server without any encoding in Java, then it worked.
I had a similar problem and was receiving the java.lang.IllegalArgumentException from Apple when trying to validate a receipt on my server. The problem was that my base64 encoding logic was inserting lines breaks into the encoded string. Once I updated my code to ensure no new line breaks were being inserted into the encoded string, I was able to successfully verify my receipts against Apple's servers.

Categories