简体   繁体   中英

Disable escaping in Android Volley

In my Android application I use escapeJson on the strings (which are created by the user) I upload to my server using StringRequest . If the strings contain a new line then the string on the server contains \\\\n instead of \\n at each new line.

StringRequest postRequest = new StringRequest(Request.Method.POST, myUrl,
        new Response.Listener(){
            @Override
            public void onResponse(Object response) {
                ...
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        ...
    }
}
){
    @Override
    protected Map<String, String> getParams()
    {
        Map<String, String>  params = new HashMap<String, String>();
        params.put("text", StringEscapeUtils.escapeJson(text));
        return params;
    }
};

If I type some lines of text in my textview and send it to the server using code above each new line is represented by \\\\n when it should be \\n . It seems like StringRequest is escaping each \\n , can I disable that?

I believe the problem here is in escapeJson; I suggest that instead of allowing it to escape your string, use URLEncode in its place and URLDecode on the server.

Edit

Pre-converting the carriage return to Unicode before using escapeJson is also an option.

String modifiedText = originalText.replaceAll("\\n", "\\u000D");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM