Sending special characters (ë ä ï) in POST body with DataOutputStream in Android

谁说胖子不能爱 提交于 2020-01-03 21:09:10

问题


Im currently working on an Android app with heavy server side communication. Yesterday I got a bug report saying that the users aren't able to send (simple) special characters such as ëäï.

I searched but didn't find anything helpful Possible duplicate ( without answer ): https://stackoverflow.com/questions/12388974/android-httpurlconnection-post-special-charactes-to-rest-clint-in-android

My relevant code:

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 is a String ;-)

Hope we can solve this together

UPDATE:

Tried:


writeUTF()

need a server capable of understanding the modified UTF-8


byte[] buf = body.getBytes("UTF-8");
dos.write(buf, 0, buf.length);

strings work but no special chars

update: Got it working with StringEntity(* string, "UTF-8") then parse the result to a byte[] and write it with dos.write(byte[])!

--


回答1:


Setting the encoding of the StringEntity did the trick for me:

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

seen here: https://stackoverflow.com/a/5819465/570168




回答2:


i am not totally sure buy try this utility for your case URLEncoder.encode(string, "UTF-8")




回答3:


I faced this problem in android while passing a json with special char (ñ). In my WebApi method, [FromBody] param is giving null, it seems it can't parse the json.

I got it working by getting bytes as UTF-8 then writing it in DataOutputStream (Client-side fix).

byte[] b = jsonString.getBytes("UTF-8");
os.write(b, 0, b.length);


来源:https://stackoverflow.com/questions/13641828/sending-special-characters-%c3%ab-%c3%a4-%c3%af-in-post-body-with-dataoutputstream-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!