'How to add non-English words as text part to multi-part request in volley lib?

I have a custom json request class to upload multi-part files to server this class in an Android application , it works fine when I add English words as keys and values(these keys and values are one part) but when I use any Arabic word, it writes unknown words like "'D*F*9".
Before I write these values to ByteArrayOutputStream object, every thing works fine, and I can see Arabic words.
This is the get Body Content Type method for the all class

@Override
public String getBodyContentType() {
    return "multipart/form-data;charset=UTF-8;boundary=" + boundary;
    //return "application/json; charset=UTF-8";
    //return "multipart/form-data; charset=UTF-8";
}



Here is the build Text Part method to construct the text part

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



And here the code I have used to debug these values

// just for debugging
        DataInputStream in = new DataInputStream(new ByteArrayInputStream  (bos.toByteArray()));
        Scanner scanner = new Scanner(in);
        while (scanner.hasNext()){
            Log.d("scanner ", scanner.next());
        }



I have read this RFC document but I can't understand what the purpose of Content-Disposition and what does its values mean.


What should I edit in this code to get the Arabic values correctly ?

Should I change the content type in all parts or I can make every part has its content Type ?

Any help plz ?



Solution 1:[1]

Finally I found the answer, the problem was in this line

dataOutputStream.writeBytes(parameterValue + lineEnd);

So I updated it to

dataOutputStream.write(parameterValue.getBytes("utf-8"));
dataOutputStream.writeBytes(lineEnd);

to get all un ASCII characters, and now it works fine for all languages.

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Khalid Ali
Solution 2 Suraj Rao