Android 如何使用HttpPost发送日语字符
我正试图将日语字符发送到API服务器,但发送的字符被乱码,变成了Android 如何使用HttpPost发送日语字符,android,unicode,encoding,character-encoding,http-post,Android,Unicode,Encoding,Character Encoding,Http Post,我正试图将日语字符发送到API服务器,但发送的字符被乱码,变成了?。因此,我使用以下方法将编码设置为实体: StringEntity stringEntity = new StringEntity(message, "UTF-8"); 但是输出变成了org.apache.http.entity。StringEntity@4316f850。我想知道将stringEntity转换为string是否会导致这种情况,因为我想将其作为string发送到服务器 下面是我如何使用它的: public
?
。因此,我使用以下方法将编码设置为实体:
StringEntity stringEntity = new StringEntity(message, "UTF-8");
但是输出变成了org.apache.http.entity。StringEntity@4316f850
。我想知道将stringEntity
转换为string是否会导致这种情况,因为我想将其作为string
发送到服务器
下面是我如何使用它的:
public static String postSendMessage(String path, String message) throws Exception {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 10000); // Timeout limit
HttpPost httpPost = new HttpPost(SystemInfo.getApiUrl() + path);
List<NameValuePair> value = new ArrayList<NameValuePair>();
StringEntity stringEntity = new StringEntity(message, "UTF-8");
value.add(new BasicNameValuePair("message", stringEntity.toString())); //Here's where I converted the stringEntity to string
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();
String result = convertStreamToString(is);
return result;
}
公共静态字符串postSendMessage(字符串路径,字符串消息)引发异常{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),10000);//超时限制
HttpPost HttpPost=新的HttpPost(SystemInfo.getApiUrl()+路径);
列表值=新的ArrayList();
StringEntity StringEntity=新的StringEntity(消息“UTF-8”);
value.add(新的BasicNameValuePair(“message”,stringEntity.toString());//这里是我将stringEntity转换为字符串的地方
UrlEncodedFormEntity实体=新的UrlEncodedFormEntity(值);
httpPost.setEntity(实体);
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
InputStream=httpEntity.getContent();
字符串结果=convertStreamToString(is);
返回结果;
}
哪里会出错?您不需要使用
StringEntity
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("message", message));
谢谢,我的服务器现在已经接受了字符。但我有相关的问题,在我的应用程序中,我无法下载带有日语文件名的文件,例如名为-ははは.pdf。有没有办法设置下载的编码?”这是代码。@Compaqle202x:您不会显示从哪里获取
url
或pdfFilename
,但通常url
作为URI必须是纯ASCII。如果您有一个带有非ASCII字符的IRI,则需要首先将其转换为URI;对于通过对字符的UTF-8字节进行%编码完成的路径部分URLEncoder.encode(文件名,“UTF-8”)
几乎可以做到这一点(除非它有空格错误,您可以用%20
替换+
,以解决这个问题)。@bobince实际上PdfileName
只是带有扩展名的文件名,url是路径[0]
,例如(http://192.168.xx.xx/resources/upload/pdf/ははは.pdf
)。但是当我把它粘贴到浏览器中时,它变成了(http://192.168.xx.xx/resources/upload/pdf/%E3%81%AF%E3%81%AF%E3%81%AF.pdf
)。正如您所说,我将路径[0]转换为URI
,然后再转换为URL
,但是什么也没发生。我还尝试了String urlEncoded=URLEncoder.encode(路径[0],“UTF-8”)代码>但未找到触发的协议
。你能给我更多的启发吗?
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value, "UTF-8");