相关疑难解决方法(0)

将UTF-8编码数据发布到服务器会丢失某些字符

我正在开发项目,其中包括服务器(JavaEE app)和客户端(Android app)的通信.XML作为HTTP请求的POST参数之一发送(名为"xml").我传递给服务器的其他POST参数也很少,但在下面的功能中,为了简单起见,我删除了它们.出现的问题是某些字母未正确传送到服务器 - 例如字符?(请注意,这不是德语Ü,顺便说一下,它是正确传送的).发送代码如下:

private String postSyncXML(String XML) {
    String url = "http://10.0.2.2:8080/DebugServlet/DebugServlet";
    HttpClient httpclient = new DefaultHttpClient();  

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("xml",XML));

    UrlEncodedFormEntity form;
    try {
        form = new UrlEncodedFormEntity(nameValuePairs);
                form.setContentEncoding(HTTP.UTF_8);
        HttpPost httppost = new HttpPost(url);

        httppost.setEntity(form);

        HttpResponse response = (HttpResponse) httpclient .execute(httppost);
        HttpEntity resEntity = response.getEntity();  
        String resp = EntityUtils.toString(resEntity);
        Log.i(TAG,"postSyncXML srv response:"+resp);
        return resp;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) …
Run Code Online (Sandbox Code Playgroud)

xml android utf-8 special-characters

42
推荐指数
2
解决办法
5万
查看次数

标签 统计

android ×1

special-characters ×1

utf-8 ×1

xml ×1