Android Kotlin - Volley:发送文件以及包含表情符号的文本
Android Kotlin - Volley: sending file along with text that contains emojis
当我只发送带有表情符号的文本时,例如:
val request = object: StringRequest(Method.POST, url, Response.Listener<String> {
//
}, Response.ErrorListener { _ ->
//
})
{
override fun getParams(): MutableMap<String, String> {
return parameters
}
}
然后它工作正常,但是当我尝试使用自定义 class 将它与文件一起发送时:https://gist.github.com/ycui1/5d25672430e6c014a9ef6b422f82652e
像那样:
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
}
}
然后表情符号从 =)
等等。
这是为什么?我需要更改什么才能像没有文件一样将文本与文件一起发送?
通过使用这个 class 解决了这个问题:https://gist.github.com/anggadarkprince/a7c536da091f4b26bb4abf2f92926594
并改变 buildTextPart
至:
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);
}
当我只发送带有表情符号的文本时,例如:
val request = object: StringRequest(Method.POST, url, Response.Listener<String> {
//
}, Response.ErrorListener { _ ->
//
})
{
override fun getParams(): MutableMap<String, String> {
return parameters
}
}
然后它工作正常,但是当我尝试使用自定义 class 将它与文件一起发送时:https://gist.github.com/ycui1/5d25672430e6c014a9ef6b422f82652e
像那样:
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
}
}
然后表情符号从 =)
等等。
这是为什么?我需要更改什么才能像没有文件一样将文本与文件一起发送?
通过使用这个 class 解决了这个问题:https://gist.github.com/anggadarkprince/a7c536da091f4b26bb4abf2f92926594
并改变 buildTextPart
至:
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);
}