It is necessary by means of http to send an object to which the array is included. Reread and google already a lot. I can not understand how to send it. As far as I understand, this byte array needs to be cast into a string and send it.

- How to get a byte array of a serialized object?

/* Псевдокод */ array[][] ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(array); 
  • maybe you need to send the object as body parameters? - Yury Pashkov
  • @YuryPashkov No, the server is so configured (not by me) what needs to be transmitted exactly how ?api=array=(и тут сам объект) This is just an example. - Ilya Shlyahovoy
  • and should the object be serialized as a String? - Yury Pashkov
  • @YuryPashkov the byte array needs to be transferred like "array=" + bytearray.toString; - Ilya Shlyahovoy

1 answer 1

For a complete answer to a question, it is desirable to know the server API (in what form an object is expected) and to have an idea about the object being sent. But I will try to give a detailed answer based on the possible options.

Option 1.
The server expects an array in a URL in one of the following formats.

  • Comma-delimited values. Example: http://ex.com/doIt?array=el1,el2,el3
  • Duplicate parameter name. Example: http://ex.com/doIt?array=el1&array=el2&array=el3

These options are suitable for transmitting an array of primitive types.

Option 2.
The server expects a JSON array or object encoded with a URLEncoder or something similar. Example: http://ex.com/doIt?array=%5B%22el1%22%2C%22el2%22%2C%22el3%22%5D
Suitable for complex types.

Option 3.
The server is waiting for the object in binary form encoded in the Base64 string. Example: http://ex.com/doIt?array=YmFzZTY0U3RyaW5n
For encoding, there are standard implementations in Java 8 and Android, and third-party implementations, for example, from Apache .

Off-topic
The following does not apply to requests that do not change the status of data on the server (filtering, etc.)
Sending an object to save it to the server using a GET request is a bad design HTTP (or REST as someone like) server API. For several reasons:

  • The GET method by definition of a standard must be idempotent . Those. side effects from N> 0 identical requests are the same as from a single request.
  • There are restrictions on the length of the URL browsers, proxies and web servers.
  • GET requests can be cached.
  • A search engine crawler that somehow received such a link can do a lot of duplicates in your database.
  • In fact, the correct option is at number 3. But, I can't find how to translate an array into a byte sequence. As I think, it is necessary to make the operation array[][] ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(array); array[][] ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(array); And then get an array of bytes baos.toByteArray and then transfer to Base64. Right i think? - Ilya Shlyahovoy
  • Correctly if the server is expecting a serialized Java object. - Ilya