I can't find a way to send binary file jpg with json Rest. The last one that I tried is with Base64.encodeToString(byte[] array, Base64.DEFAULT) and I expected in the server side with Base64.getDecoder().decode(String)... yet no results. I have seen many posts, but nothing has helped. Can you suggest me any way or library.
I am using java with Eclipse. In the client side I use android.util.Base64 library, while in server side I use java.util.Base64.
The error that show me is this:
Unexpected padding character ('=') as character #4 of 4-char base64
unit: padding only legal as 3rd or 4th character; nested exception is
com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot
deserialize value of type byte[] from String
Related
I guess this is a stupid question with an obvious solution, but I don't see it yet. So the problem is: I get an IllegalArgumentException on Android, which says my base64 input is not valid. This input took the following way before:
Upload: PDF file -(Java Base64 encoder) > Java Base64 encoded string -(POST)-> PHP -(INSERT as mediumtext via mysqli query)-> MySQL DB
Download: MySQL record -(SELECT via mysqli query and fetch assoc afterwards)-> PHP vars -(JSON)-> Java as JSON -(Jackson library, maps JSON to object containing String)-> Java Base64 String - (Android Base64 decoder)-> Exception
Is there any failure in my workflow? Communication is done with UTF-8 via HttpUrlConnection.
I was able to solve the problem: During transfer to the server + and / got omitted. After manually replacing them, everything is working now.
I have a Java script, that will get the BLOB data from the database and then email this file to a specific email address. My problem is, that I have to use some framework functions (I can make DB calls only through these) and I think it's not handling BLOB datatypes... All I can get is the string representation of the result, this is the log line result of the code (framework call):
String s = String.valueOf(result.get(j).getValue("BLOB_DATA"));
System.out.println(s);
Log result:
<binary data> 50 KB
So this is the data I have to convert SOMEHOW into a valid pdf file, but right now I'm stuck...
Is it even possible to convert it into a valid byte[]? I tried it several ways, but all I get is invalid files... :(
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)));
}
I've created a JSON string in a php file. I've then used json_encode($jsonStr) to encode the string.
$jsonStr =
"{
\"statusCode\": 0,
\"errorMsg\": \"SUCCESS\",
\"id\": $id,
\"message\": ".json_encode($message).",
\"author\": \"$author\",
\"showAfter\": \"$date\"
}";
I'm making a network call in java (Android) to get this string. My next step is to decode the string, however this doesn't seem to be working too well.
Here is a sample of what I'm trying to decode in my Android code:
{\n\t\t\t\t\"statusCode\": 0,\n\t\t\t\t\"errorMsg\": \"SUCCESS\",\n\t\t\t\t\"id\": 1,\n\t\t\t\t\"message\": \"This is a message.\",\n\t\t\t\t\"author\": \"Anonymous\",\n\t\t\t\t\"showAfter\": \"2013-06-18 01:19:49\"\n\t\t\t}
Yes it is riddled with encoded line breaks and such. I assumed that might be the issue so I took those out, however I still have issues, so I'm guessing there must be something bigger going on.
I know this is valid JSON because I'm able to decode it and use it in a javascript based website.
How can I accomplish this on Android/Java?
Your original JSON string (the one you show in your first snippet) looks to be valid JSON already. You must not encode it. Encoding it is what makes it invalid JSON, transforming every tab into \t, and every new line into \n.
Read the documentation of json_encode carefully.
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.