Android default charset when sending http post/put - Problems with special characters

后端 未结 4 1633

I have configured the apache httpClient like so:

HttpProtocolParams.setContentCharset(httpParameters, \"UTF-8\");
HttpProtocolParams.setHttpElementCharset(ht         


        
相关标签:
4条回答
  • 2020-12-08 09:05

    You can eliminate the server as the problem by using curl to send the same data. If it works with curl use --trace to check the output.

    Ensure you are sending the content body as bytes. Compare the HTTP request from Android with the output from the successful curl request.

    0 讨论(0)
  • 2020-12-08 09:07

    I know this post is a bit old but nevertheless here is a solution:

    Here is my code for posting UTF-8 strings (it doesn't matter if they are xml soap or json) to a server. I tried it with cyrillic, hash values and some other special characters and it works like a charm. It is a compilation of many solutions I found through the forums.

    HttpParams httpParameters = new BasicHttpParams();
    HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8);
    HttpProtocolParams.setHttpElementCharset(httpParameters, HTTP.UTF_8);
    
    HttpClient client = new DefaultHttpClient(httpParameters);
    client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
    client.getParams().setParameter("http.socket.timeout", new Integer(2000));
    client.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
    httpParameters.setBooleanParameter("http.protocol.expect-continue", false);
    HttpPost request = new HttpPost("http://www.server.com/some_script.php?sid=" + String.valueOf(Math.random()));
    request.getParams().setParameter("http.socket.timeout", new Integer(5000));
    
    List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    // you get this later in php with $_POST['value_name']
    postParameters.add(new BasicNameValuePair("value_name", "value_val"));
    
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, HTTP.UTF_8);
    request.setEntity(formEntity);
    HttpResponse response = client.execute(request);
    
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer sb = new StringBuffer("");
    String line = "";
    String lineSeparator = System.getProperty("line.separator");
    while ((line = in.readLine()) != null) {
        sb.append(line);
        sb.append(lineSeparator);
    }
    in.close();
    String result = sb.toString();
    

    I hope that someone will find this code helpful. :)

    0 讨论(0)
  • 2020-12-08 09:11

    You should set charset of your string entity to UTF-8:

    StringEntity stringEntity = new StringEntity(urlParameters, HTTP.UTF_8);
    
    0 讨论(0)
  • 2020-12-08 09:14

    Apparently, I forgot to set the StringEntity's charset to UTF-8. These lines did the trick:

        httpPut.setEntity(new StringEntity(body, HTTP.UTF_8));
        httpPost.setEntity(new StringEntity(body, HTTP.UTF_8));
    

    So, there are at least two levels to set the charset in the Android client when sending an http post with non-ascii characters.

    1. The rest client itself itself
    2. The StringEntity

    UPDATE: As Samuel pointed out in the comments, the modern way to do it is to use a ContentType, like so:

        final StringEntity se = new StringEntity(body, ContentType.APPLICATION_JSON);
        httpPut.setEntity(se);
    
    0 讨论(0)
提交回复
热议问题