Problem with decoding Base64 bitmap that sent to server via http request - android

We are trying to call a post request via HTTP call in android, the library that we are using is Retrofit.
First of all, we encode our Bitmap image to base64 with the following code.
public static String toBase64(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT);
}
And then we send our data using Retrofit to our server.
Our backend is written both with Spring and Flask frameworks and both servers behave exactly the same.
We've tried to debug and find the problem. We found that some weird character is added to the request's body sometimes. It is not persistent but sometimes we got errors on server side.
As you can see in the picture some characters are replaced with some weird characters that aren't in UTF-8 charset. Also, we have this issue on iOS platforms too and I should mention that the size of the image is around 1MB.
The 'Content-Type' header is set correctly like content-type: application/json; UTF-8.
I would really appreciate it if someone could help.

Related

Decode a string encoded with Brotli returned by Android Volley

I'm sending an http request to a website using Volley (POST and StringRequest). The call is correctly executed. However, I can see that the result is a string codified. When seeing the headers of the answer I can see it is encoded in br, which means brotli. How can I decode the answer to later read it as a JSON?
Should I change to OkHttp or another alternative?

Upload Image From Android to ASp.net Server

I am trying to upload image fromandroid to asp.net server.
I am following following procedure.
convert image from bitmap to byte[] yhen byte[] to string and pass this string to the asp.net web service
is this correct way to save image on to the .net server
Please Give the solution to upload image from android to asp.net server
both client and server side code .
The simplest way is convert your bitmap to base64 string :
public String encodeTobase64(Bitmap image)
{
Bitmap immagex=image;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
return imageEncoded;
}
after converting ,you can send the string to webservice, on the server side, you must decode this string to an image !
To convert base64 to image on server side,duckduckgo or google will help you :
https://duckduckgo.com
https://www.google.com/search?sclient=psy-ab&site=&source=hp&btnG=Search&q=convert+base64+to+image+ASp.net+Server
Have a look at this tutorial by Microsoft - you'll be able to use the Build the Web Service part for the web service.
For the Android part, you can get inspired by all other questions that try to send a POST request - for example this one.
If not too late, here is my suggested solution:
Server side: you can create a web service or RESTful application on the .NET Framework. Microsoft provides ASP.NET Web API. You can start from their website here
Client side: your Android here. You can refer to some of the following: Volley, Retrofit...
If you want to use Volley, you can refer to some following links:
Working POST Multipart Request with Volley and without HttpEntity
How to send a “multipart/form-data” POST in Android with Volley
Trouble Sending Multipart File with Boundary via Volley
Of course, you can find more available in SO.
Hope this helps!

Sending Image to server in android

In android I am trying to send image to the server.I am using Multipart request. But when I add image in format ByteArray to the Multipart UnsupportedEncodingException occurs.
following is my code.
Do I need to add mime type to request.
If possible please post the complete Sample code.
Try converting the Image to Base64 format string and then sending that string to the server.
Here's the link to convert image to base64 format.
And then at server end decode it.
I done it with the use of MultiPartEntity . Added image as byteArrayBody and done the HTTP request.

Encode XML string to EXI and send it through a websocket

First of all I am using a client-server architecture, android for the client and node.js for the server, they are connected through Socket.io library, so, they are using websockets.
The doubt that I have is that I am generating a XML string with XMLSerializer from Java, I want to encode it to EXI and send it to a server, thus, is it possible doing the encode XML-EXI without using files? directly from string to string? because all examples I see assume that my XML is in a file and I want the output into another file. Another doubt is, can I just send the EXI as string? because I have already established the communication between the client and the server, but they just send strings, I don not if I can sent whole files, in that case, would be any diference on the amount of data sent?
Finally I have solved it, for people with the same problem, the solution is:
String input = methodGivingXMLString();
byte inputBytes[] = input.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(inputBytes);
transmogrifier.encode(new InputSource(in));
For the input, and for the output:
ByteArrayOutputStream result = new ByteArrayOutputStream();
transmogrifier.setOutputStream(result);
note 1: I am using OpenExi library
note 2: The output stream has to be set before calling the encode() method.

android post gzip

Trying to implement gzip on an Android project to reduce client data charges. All devices connect to a WCF webservice and IIS is now sending compressed data back to the devices as expected. Now I need to work out how to post back gzipped xml data modified on the android device.
The device code is as follows
httpsURLConnection.setDoInput(true);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.setConnectTimeout(TIMEOUT);
httpsURLConnection.setReadTimeout(TIMEOUT);
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setRequestProperty("Content-Type", "application/xml");
httpsURLConnection.setRequestProperty("Content-Encoding", "gzip);
httpsURLConnection.connect();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(new BufferedOutputStream((httpsURLConnection.getOutputStream())));
gzipOutputStream.write(_xmlText.getBytes());
gzipOutputStream.flush();
gzipOutputStream.finish();
gzipOutputStream.close();
Running Wireshark on webserver shows the gzip packets and decompresses them to show the correct data from the device.
The problem is that the WCF web service does not seem to recognise the data - the error is XmlException - The data at the root level is invalid. Line 1, position 1.
Which makes me think that the data is still compressed and WCF cannot handle gzip - which I seem to remember reading about previously. Do I then need to create a message decoder in .net to handle the gzip compression? Was this hopefully addressed in .net 4.5?
Any help with these questions appreciated.

Categories

Resources