android dataoutputstream 中文,使用Android中的DataOutputStream在POST正文中发送特殊字符(ëäï)...

我目前正在开发一款具有大量服务器端通信功能的 Android应用.昨天我收到一个错误报告,说用户无法发送(简单)特殊字符,例如ëäï.

我搜索过但没有找到任何帮助

可能重复(没有回答):

https://stackoverflow.com/questions/12388974/android-httpurlconnection-post-special-charactes-to-rest-clint-in-android

我的相关代码:

public void execute(String method) {

HttpsURLConnection urlConnection = null;

try {

URL url = new URL(this.url);

urlConnection = (HttpsURLConnection) url.openConnection();

urlConnection.setRequestMethod(method);

urlConnection.setReadTimeout(30 * 1000);

urlConnection.setDoInput(true);

if (secure)

urlConnection.setRequestProperty("Authorization", "Basic " + getCredentials());

if (body != null) {

urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

urlConnection.setFixedLengthStreamingMode(body.length());

urlConnection.setDoOutput(true);

DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());

dos.writeBytes(body);

dos.flush();

dos.close();

}

responseCode = urlConnection.getResponseCode();

message = urlConnection.getResponseMessage();

InputStream in = null;

try {

in = new BufferedInputStream(urlConnection.getInputStream(), 2048);

} catch (Exception e) {

in = new BufferedInputStream(urlConnection.getErrorStream(), 2048);

}

if (in != null)

response = convertStreamToString(in);

} catch (UnknownHostException no_con) {

responseCode = 101;

}catch (ConnectException no_con_2){

responseCode = 101;

}catch(IOException io_ex){

if(io_ex.getMessage().contains("No authentication challenges found")){

responseCode = 401;

}else

responseCode = 101;

} catch (Exception e) {

e.printStackTrace();

} finally {

if (urlConnection != null)

urlConnection.disconnect();

}

}

身体是一个字符串;-)

希望我们能一起解决这个问题

更新:

尝试:

writeUTF()

需要一台能够理解修改过的UTF-8的服务器

byte[] buf = body.getBytes("UTF-8");

dos.write(buf, 0, buf.length);

字符串工作,但没有特殊的字符

更新:使用StringEntity(* string,“UTF-8”)然后将结果解析为byte []并用dos.write(byte [])写入!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值