Encode XML string to EXI and send it through a websocket - android

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.

Related

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

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.

Android WebRTC Receiving a JSON from a javascript PeerJS browser

my app is using WebRTC to communicate with a browser using Chrome (PeerJS), actually multiple browsers. So, whenever I create a block within the browser, the peers are supposed to pass to each other the info of that browser, as far as I know it should be a JSON. The thing is, I cannot get to format it the right way, there are always messed up characters in the message I receive, even if the information like previous block are correct. I just feel like this shouldn't be happening.
I'm trying to figure out what is going on, I feel like PeerJS is doing something more other than sending just a pure JSON. When my app communicate through WebSockets for Offer/Answer/Candidate, all their JSON comes in a normal formating.
My code for onMessage for my RTC Datachannel is:
#Override
public void onMessage(DataChannel.Buffer buffer) {
Charset utf8 = Charset.forName("UTF-8");
ByteBuffer byteBuffer = buffer.data;
CharBuffer charBuffer = utf8.decode(byteBuffer);
Log.d(TAG, "onMessage: " + byteBuffer.toString());
Log.d(TAG, "onMessage: " + charBuffer.toString());
}
The messages I get from the browsers:
��typem�data��type�block��header��index�timestamp�����d�o���previousHash���#0ff530e5a7f0f7d88189e1a87c380cbe0a1a5de9a904278c4831592b0bfd7017�hash���#0d15980b550ce37fe347d08d27e7806980aa8fb65663e667c9c6630de7d69e8e�data��type�ART�timestamp�����d�o���contexthash���#a46887f22840ca5e7ac2368e1c090b3feab8f238788be71864831b48cac45a8f���requestingAcessPKey���VbBtHUR-LkiTMYpxrcF9MofNFa_fgHWLTQkpfSEvo1nksRmsUBiiG7k9eNbOjZ4IDPp61IO4BnA7hz4JiahslxM�signedMsg����0645fc574d2a2ea04018baf91f3b030dea3a4b66a862ae7ad5d6bd8c9d35ddbd18f49b853d75fd7578361046e28104bc6565c2aeb7df7aa7ea120851ea4b6fbf
Also, the message is in a binary format.
Answering my own question. The problem was that PeerJS uses a JS-binary serialization library, in order to deserialize it I needed to write that lib into Java code.
There was no way with raw Android/Java to do it.
The JS library is called binarypackJS.
(Also worth mentioning, that you can actually use JSON to communicate with peerJS, all you need to do is to enable msg type as 'json' and not 'binary'.

What is best solution file upload for python server

I programming python server and android client.
program's logic is client send multiple file to server.
I first try C/C++ socket server but receive error. so I change python. because server on raspberry pi.
I have to implement file upload, audio streaming. so I think this logic.
1. Client send http request to server
2. When server receives the request, server create tcp socket and listen.
3. Client receives success response, connect to server and file upload.
Audio streaming will implement similar way.
Is it ok to implement this way? or is there a better way?
Please give me a hint how to implement it.
Ignore socket as much as you for small deployments. They are little more complicated to handle.
Now asuming you want to upload a image file or some other file to a python you can use Flask Upload
Next move on to audio, if you have to upload audio from client to server, than there is no need to stream or stuff, just pass appropriate MIME type during upload.
ALLOWED_AUDIO_EXTENSIONS = set(['wav', 'ogg', 'mp3', ])
def audio_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_AUDIO_EXTENSIONS
if file and audio_file(file.filename):
filename = secure_filename(file.filename)
#perform some application logic with audio files and then save them in file system or call boto3 to save on s3
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
Code is modified from flask examples.

Decoding payload msg going through lorawan multitech conduit gateway

I managed to get a node to communicate to an android app. The message goes from the node to the gateway. Using node-red the gateway publishes the message to an mqtt broker and I subscribe to the right topic to receive that message on my android app.
Only problem I have now is that I never realized that the msg was encoded and I'm quite lost now. I receive an array of bytes on the app side and I'm not sure what is the next step I have to take to be able to read that message.
Thanks for the help!
You are actually receiving bytes from Node-Red. You could either convert the received bytes into String on your Android or modify your lora input node's data type from Bytes to UTF-8.
I have a function to decode it:
var x = Buffer.from(msg.payload.raw,"base64").toString()
msg.Decrypted = parseFloat(x)
return msg;
In this Instructable i explained how to decode Lora messages in node red
https://www.instructables.com/id/Lora-Temperature-Dashboard/
On a MultiTech Conduit, the message is base64 encoded by the internals of the MTC, then published to the internal mqtt broker at lora//up, so you need to base64 decode it, then do whatever you need to with it. You can use the built in mosquitto applications to subscribe to this topic, or any other mqtt client (paho libraries, etc)

Mosquitto publish message format to android app

I am working on a IoT project where i'm using mosquitto broker to get messages to my android app. But when i receive the published messages on my app, they appear to be in a weird format. how can i get the data in correct format. Do i need to change something in mosquitto configuration.
The data sent: "hello"
Received on app: [B#2df51a89
this is what i use to get the text in onMessageReceived(topic,message)
message.getPayload().toString();
Any help is appreciated.
[UPDATE] - When i publish data FROM mobile app, the data is received perfectly at the subscriber. Vice versa doesn't work.
You've printed out the object representation of the byte array (message payload). The correct thing to have done is to create a new String from the byte array using a known encoding (probably UTF8)
String payload = new String(message.getPayload(), "utf8")
Then print the payload String

Categories

Resources