如何在凌空库中将非英语单词作为文本部分添加到多部分请求中? [英] How to add non-English words as text part to multi-part request in volley lib?
问题描述
我有一个自定义的json请求类,可以将多部分文件上传到Android应用程序中的该类服务器,当我添加英文单词作为键和值(这些键和值是一部分)但我使用时,它可以正常工作任何阿拉伯语单词,它都会写入未知单词,例如'D * F * 9".
在将这些值写入ByteArrayOutputStream对象之前,一切正常,并且可以看到阿拉伯语单词.
这是所有类
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";
}
这是构建文本部分的build文本部分方法
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());
}
我已经阅读过 RFC文档,但我不明白Content-的目的是什么-处置及其含义是什么.
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 ?
有什么帮助吗?
推荐答案
最后我找到了答案,问题出在这一行
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);
获取所有非ASCII字符,现在它适用于所有语言.
to get all un ASCII characters, and now it works fine for all languages.
这篇关于如何在凌空库中将非英语单词作为文本部分添加到多部分请求中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!