Java-MySQL-Android: bad base-64 - java

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.

Related

Sending binary files jpg from android with Json Rest Services

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

MySQL - Java - Can't handle special characters

I have a JTextArea from which I get the text, and send it through a POST request to a PHP page, which uses PDO and a prepare statement to insert the text into a MySQL database. This works for everything that I can type directly into the JTextArea. However, it doesn't work when I copy and paste something from Word, for example, into the JTextArea and then upload it.
For example, if I type into Word: "This is a super—long dash." Then copy and paste that in the JTextArea and have it send to the PHP page, which will upload it to the MySQL server, when I get the information back from the MySQL server (using another PHP page) it comes back as "This is a super—long dash."
This also happens for other various special characters, not just long dashes.
Is there anything I can do about this?
EDIT:
I tried to correct the issue by making sure all my php files are using utf8 to connect to mysql like so:
"mysql:host=$servername;dbname=$dbname;charset=utf8"
I also use
echo $_POST[xxx];
To make sure the text is coming in from java correctly, which it is. When I check what is the mysql database, it shows as "This is a superùlong dash". I tried to correct this by using the fixUTF8 function in the solution here: Detect encoding and make everything UTF-8 by Sebastián Grignoli. However, as a result I get "This is a super?long dash".

How to convert binary data back into a valid file?

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... :(

UTF-8 encoding problems between Android, JSon and PHP

I programmed an Android App, which manages data to be stored and deleted in a MySQL database (on server). Whenever on the smartphone special characters ("ä", "ü", ...) are used the symbol is converted badly.
In the log I can see that the "Umlaut" (e.g. "ä") is transmitted properly. I also use in my php file "SET NAMES 'UTF-8'", see here:
function connect()
{
....
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
mysql_query("SET NAMES 'utf8'");
// Selecting database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
return $con;
}
If I start a request via Postman, I can add words with special characters, e.g. "TÄST", the json response looks as follows:
{"catId":"35","category":"T\u00c4ST"}
So the words are well converted to UTF-8. But if I add an Item via smartphone, the response from the server (to retrieve the added item) looks like this:
{"catId":"37","category":"T?ST"}
The position in my code, where I add the parameters for the JSON Object is that (note that "name" is the string content of the edit text field):
List<NameValuePair> params = new ArrayList<NameValuePair>();
...
params.add(new BasicNameValuePair("category", catName));
After that the HTTPRequest is send:
JSONObject json = jsonParser.makeHttpRequest(url_dest, "POST", params);
If I print out the params the word "Täst" is visible... But unfortunately I'm not able to check which json string is arriving on the server (due to my bad php knowledge). So where's the problem? In my android application or in the php files located on the server?
Do I have to encode the outgoing json object in any way?
Thanks a lot for your help.
To debug on Eclipse I suggest you:
http://projects.eclipse.org/projects/tools.pdt
It is quite easy to configure.
Control the encoding in the DB; maybe the VARCHAR default latin1_swedish_ci can't save that string.
Another way to solve it could be looking to the apache commons:
http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
in particular the functions provided by: escapeHtml4 and unescapeHtml4
#dafocus, if you set your Android app project to UTF-8 encoding in your IDE, it should ensure that UTF-8 is handled properly in the app. It looks like an encoding change somewhere. You might not need to convert TÄST to T\u00c4ST at all if the encoding is UTF-8 throughout. PHP used to be difficult with charsets. I would suggest you to look at the PHP to figure out if the handler page causes this (possible) and/or the DB - as #madx just said.

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