Search code examples
androidkotlinandroid-volleyemoji

Android Kotlin - Volley: sending file along with text that contains emojis


When I'm sending only text that has emojis, like that:

        val request = object: StringRequest(Method.POST, url, Response.Listener<String> {
            //

        }, Response.ErrorListener { _ ->
            //
        })
        {
            override fun getParams(): MutableMap<String, String> {
                return parameters
            }
        }

then it works fine, but when I try sending it along with a file using custom class: https://gist.github.com/ycui1/5d25672430e6c014a9ef6b422f82652e

like that:

    val request = object: VolleyFileUploadRequest(Method.POST, url, Response.Listener {
        //
    },
        Response.ErrorListener {
            //
        }
    ) {
        override fun getByteData(): MutableMap<String, FileDataPart> {
            val params = HashMap<String, FileDataPart>()
            params["file"] = FileDataPart(
                "file$rndInt",
                getBytes(finalInputSteam!!)!!,
                "fffff"
            )
            return params
        }

        override fun getParams(): MutableMap<String, String> {
            return parameters
        }
    }

then the emojis are becoming from 😂 to =) and so on.

Why is that? What do I need to change to send the text along with file like it would be without?


Solution

  • Solved it by using this class instead: https://gist.github.com/anggadarkprince/a7c536da091f4b26bb4abf2f92926594

    and changing buildTextPart to:

    private void buildTextPart(DataOutputStream dataOutputStream, String parameterName, String parameterValue) throws IOException {
        dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
        dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"");
        dataOutputStream.write(parameterName.getBytes("UTF-8"));
        dataOutputStream.writeBytes(lineEnd);
        dataOutputStream.writeBytes("Content-Type: text/plain; charset=UTF-8" + lineEnd);
        dataOutputStream.writeBytes(lineEnd);
        dataOutputStream.write(parameterValue.getBytes("UTF-8"));
        dataOutputStream.writeBytes(lineEnd);
    }