在Android中使用DataOutputStream在POST正文中发送特殊字符(ë;ä;ï;)

在Android中使用DataOutputStream在POST正文中发送特殊字符(ë;ä;ï;),android,json,rest,post,Android,Json,Rest,Post,我目前正在开发一款Android应用程序,服务器端通信量很大。昨天我收到一个bug报告,说用户不能发送(简单的)特殊字符,比如ëëï 我搜索了一下,但没有找到任何有用的东西 可能重复(无答案): 我的相关代码: public void execute(String method) { HttpsURLConnection urlConnection = null; try { URL url = new URL(this.url);

我目前正在开发一款Android应用程序,服务器端通信量很大。昨天我收到一个bug报告,说用户不能发送(简单的)特殊字符,比如ëëï

我搜索了一下,但没有找到任何有用的东西 可能重复(无答案):

我的相关代码:

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();
        }
    }
body
是一个字符串;-)

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

更新:

尝试:


需要能够理解修改后的UTF-8的服务器


字符串可以工作,但没有特殊字符 更新:让它与StringEntity(*string,“UTF-8”)一起工作,然后将结果解析为字节[],并用dos.write(字节[])写入


--

我不能完全确定是否购买或试用此实用程序
URLEncoder.encode(字符串,“UTF-8”)

设置StringEntity的编码对我来说很有用:

StringEntity entity = new StringEntity(body, "UTF-8");

如图所示:

我在android中传递带有特殊字符(ñ)的json时遇到了这个问题。 在我的WebApi方法中,[FromBody]param给出null,它似乎无法解析json

我通过以UTF-8的形式获取字节,然后将其写入DataOutputStream(客户端修复程序)来实现它


读写字节文档。改用writeUTF。听起来很公平。我怎么知道那根绳子的长度。我现在在指定有意义的错误长度时遇到了一个错误,因为有些字符是用几个字节编码的。您可以尝试不指定长度,可以使用writeChars(但效率较低,因为所有字符都是2字节的),可以使用String.getBytes(“UTF-8”).length(但这会创建一个相当长的无用字节[])。另外,请注意DataOutputStream.writeUTF是一种自定义格式,它在开始处添加字符串的长度,并用于由DataInputStream读取。(但这取决于您的服务器)是的,我通过不指定长度来实现。但服务器不是Java语言,无法识别writeUTF修改的UTF编码。还有其他想法吗?也尝试了byte[]buf=body.getBytes(“UTF-8”);dos.write(buf,0,buf.length);但特殊字符仍不工作StringEntity=新StringEntity(正文,“UTF-8”);1) writeBytes(entity.toString());2) writeUTF(entity.toString());我两种方法都试过了,但都不管用。提前谢谢。
byte[] buf = body.getBytes("UTF-8");
dos.write(buf, 0, buf.length);
StringEntity entity = new StringEntity(body, "UTF-8");
byte[] b = jsonString.getBytes("UTF-8");
os.write(b, 0, b.length);