Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从XML到Java的字符编码_Java_Xml_Http_Encoding - Fatal编程技术网

从XML到Java的字符编码

从XML到Java的字符编码,java,xml,http,encoding,Java,Xml,Http,Encoding,如果我将字符串xml打印到屏幕上,我可以看到编码已经出现了一些问题 那么要返回一个文件,我有这个 // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpP

如果我将字符串xml打印到屏幕上,我可以看到编码已经出现了一些问题

那么要返回一个文件,我有这个

        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);
虽然我正确地从http请求中获取了信息,但在显示数据时,字符的重新编码有问题


我已经尝试过使用is.setEncoding(“UTF-8”),但没有成功。

问题是您将xml转换为字符串(字符),不要这样做(您很可能使用了错误的编码并损坏了xml)。将xml视为二进制数据(字节)


您可以使用
EntityUtils.toByteArray
(好的),也可以将HttpEntity流直接传递给xml解析器(理想)。

问题是您将xml转换为字符串(字符),不要这样做(您很可能使用了错误的编码并损坏了xml)。将xml视为二进制数据(字节)


您可以使用
EntityUtils.toByteArray
(好的),或者您可以将HttpEntity流直接传递给xml解析器(理想)。

感谢您的回复,问题不可能出在xml而不是代码上吗?我可以使用您建议的方法来代替EntityUtils.toString,但是我应该在这个对象的构造函数中放置什么呢?is.setCharacterStream(新的StringReader(xml))//在使用字符串is.setCharacterStream(???);//我不知道哪个构造函数可以用于字节数组。@unpix use。如果你从一个字节数组开始,你可以使用。谢谢,也许它也能工作,但我已经找到了最简单的solution@unpix您找到了什么解决方案?我也有这样的问题。谢谢你的回答,问题不可能是XML而不是代码的问题吗?我可以使用您建议的方法来代替EntityUtils.toString,但是我应该在这个对象的构造函数中放置什么呢?is.setCharacterStream(新的StringReader(xml))//在使用字符串is.setCharacterStream(???);//我不知道哪个构造函数可以用于字节数组。@unpix use。如果你从一个字节数组开始,你可以使用。谢谢,也许它也能工作,但我已经找到了最简单的solution@unpix您找到了什么解决方案?我也有这种问题。
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        DocumentBuilder db = dbf.newDocumentBuilder();

        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));

        doc = db.parse(is);