KSOAP 的特殊字符
我正在使用 KSOAP2 来调用 Web 服务。我收到回复,但该回复中的特殊字符未正确显示。
我怎样才能改变这一点?
编辑:
以下代码负责发送和接收数据:
package org.ksoap2.transport;
import java.util.List;
import java.io.*;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import org.ksoap2.*;
import org.xmlpull.v1.*;
/**
* A J2SE based HttpTransport layer.
*/
public class HttpTransportSE extends Transport {
private ServiceConnection connection;
/**
* Creates instance of HttpTransportSE with set url
*
* @param url
* the destination to POST SOAP data
*/
public HttpTransportSE(String url) {
super(null, url);
}
/**
* Creates instance of HttpTransportSE with set url and defines a
* proxy server to use to access it
*
* @param proxy
* Proxy information or <code>null</code> for direct access
* @param url
* The destination to POST SOAP data
*/
public HttpTransportSE(Proxy proxy, String url) {
super(proxy, url);
}
/**
* Creates instance of HttpTransportSE with set url
*
* @param url
* the destination to POST SOAP data
* @param timeout
* timeout for connection and Read Timeouts (milliseconds)
*/
public HttpTransportSE(String url, int timeout) {
super(url, timeout);
}
/**
* set the desired soapAction header field
*
* @param soapAction
* the desired soapAction
* @param envelope
* the envelope containing the information for the soap call.
* @throws IOException
* @throws XmlPullParserException
*/
public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException {
call(soapAction, envelope, null);
}
/**
*
* set the desired soapAction header field
*
* @param soapAction
* the desired soapAction
* @param envelope
* the envelope containing the information for the soap call.
* @param headers
* a list of HeaderProperties to be http header properties when establishing the connection
*
* @return <code>CookieJar</code> with any cookies sent by the server
* @throws IOException
* @throws XmlPullParserException
*/
public List call(String soapAction, SoapEnvelope envelope, List headers)
throws IOException, XmlPullParserException {
if (soapAction == null)
soapAction = "\"\"";
byte[] requestData = createRequestData(envelope);
requestDump = debug ? new String(requestData) : null;
responseDump = null;
connection = getServiceConnection();
connection.setRequestProperty("User-Agent", "kSOAP/2.0");
connection.setRequestProperty("SOAPAction", soapAction);
connection.setRequestProperty("Content-Type", "text/xml");
connection.setRequestProperty("Connection", "close");
connection.setRequestProperty("Content-Length", "" + requestData.length);
// Pass the headers provided by the user along with the call
if (headers != null) {
for (int i = 0; i < headers.size(); i++) {
HeaderProperty hp = (HeaderProperty) headers.get(i);
connection.setRequestProperty(hp.getKey(), hp.getValue());
}
}
connection.setRequestMethod("POST");
connection.connect();
OutputStream os = connection.openOutputStream();
os.write(requestData, 0, requestData.length);
os.flush();
os.close();
requestData = null;
InputStream is;
List retHeaders = null;
try {
connection.connect();
is = connection.openInputStream();
retHeaders = connection.getResponseProperties();
} catch (IOException e) {
is = connection.getErrorStream();
if (is == null) {
connection.disconnect();
throw (e);
}
}
if (debug) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[256];
while (true) {
int rd = is.read(buf, 0, 256);
if (rd == -1)
break;
bos.write(buf, 0, rd);
}
bos.flush();
buf = bos.toByteArray();
responseDump = new String(buf);
is.close();
is = new ByteArrayInputStream(buf);
}
parseResponse(envelope, is);
return retHeaders;
}
public ServiceConnection getConnection() {
return (ServiceConnectionSE) connection;
}
protected ServiceConnection getServiceConnection() throws IOException {
return new ServiceConnectionSE(proxy, url);
}
public String getHost() {
String retVal = null;
try {
retVal = new URL(url).getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return retVal;
}
public int getPort() {
int retVal = -1;
try {
retVal = new URL(url).getPort();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return retVal;
}
public String getPath() {
String retVal = null;
try {
retVal = new URL(url).getPath();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return retVal;
}
}
我可以更改哪些行来强制执行 UTF-8 编码?
I'm using KSOAP2 to invoke a web service. I am getting a response, but the special characters within that response aren't shown properly.
How can I change that?
EDIT:
Following code is responsible for sending and receiving the data:
package org.ksoap2.transport;
import java.util.List;
import java.io.*;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import org.ksoap2.*;
import org.xmlpull.v1.*;
/**
* A J2SE based HttpTransport layer.
*/
public class HttpTransportSE extends Transport {
private ServiceConnection connection;
/**
* Creates instance of HttpTransportSE with set url
*
* @param url
* the destination to POST SOAP data
*/
public HttpTransportSE(String url) {
super(null, url);
}
/**
* Creates instance of HttpTransportSE with set url and defines a
* proxy server to use to access it
*
* @param proxy
* Proxy information or <code>null</code> for direct access
* @param url
* The destination to POST SOAP data
*/
public HttpTransportSE(Proxy proxy, String url) {
super(proxy, url);
}
/**
* Creates instance of HttpTransportSE with set url
*
* @param url
* the destination to POST SOAP data
* @param timeout
* timeout for connection and Read Timeouts (milliseconds)
*/
public HttpTransportSE(String url, int timeout) {
super(url, timeout);
}
/**
* set the desired soapAction header field
*
* @param soapAction
* the desired soapAction
* @param envelope
* the envelope containing the information for the soap call.
* @throws IOException
* @throws XmlPullParserException
*/
public void call(String soapAction, SoapEnvelope envelope) throws IOException, XmlPullParserException {
call(soapAction, envelope, null);
}
/**
*
* set the desired soapAction header field
*
* @param soapAction
* the desired soapAction
* @param envelope
* the envelope containing the information for the soap call.
* @param headers
* a list of HeaderProperties to be http header properties when establishing the connection
*
* @return <code>CookieJar</code> with any cookies sent by the server
* @throws IOException
* @throws XmlPullParserException
*/
public List call(String soapAction, SoapEnvelope envelope, List headers)
throws IOException, XmlPullParserException {
if (soapAction == null)
soapAction = "\"\"";
byte[] requestData = createRequestData(envelope);
requestDump = debug ? new String(requestData) : null;
responseDump = null;
connection = getServiceConnection();
connection.setRequestProperty("User-Agent", "kSOAP/2.0");
connection.setRequestProperty("SOAPAction", soapAction);
connection.setRequestProperty("Content-Type", "text/xml");
connection.setRequestProperty("Connection", "close");
connection.setRequestProperty("Content-Length", "" + requestData.length);
// Pass the headers provided by the user along with the call
if (headers != null) {
for (int i = 0; i < headers.size(); i++) {
HeaderProperty hp = (HeaderProperty) headers.get(i);
connection.setRequestProperty(hp.getKey(), hp.getValue());
}
}
connection.setRequestMethod("POST");
connection.connect();
OutputStream os = connection.openOutputStream();
os.write(requestData, 0, requestData.length);
os.flush();
os.close();
requestData = null;
InputStream is;
List retHeaders = null;
try {
connection.connect();
is = connection.openInputStream();
retHeaders = connection.getResponseProperties();
} catch (IOException e) {
is = connection.getErrorStream();
if (is == null) {
connection.disconnect();
throw (e);
}
}
if (debug) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[256];
while (true) {
int rd = is.read(buf, 0, 256);
if (rd == -1)
break;
bos.write(buf, 0, rd);
}
bos.flush();
buf = bos.toByteArray();
responseDump = new String(buf);
is.close();
is = new ByteArrayInputStream(buf);
}
parseResponse(envelope, is);
return retHeaders;
}
public ServiceConnection getConnection() {
return (ServiceConnectionSE) connection;
}
protected ServiceConnection getServiceConnection() throws IOException {
return new ServiceConnectionSE(proxy, url);
}
public String getHost() {
String retVal = null;
try {
retVal = new URL(url).getHost();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return retVal;
}
public int getPort() {
int retVal = -1;
try {
retVal = new URL(url).getPort();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return retVal;
}
public String getPath() {
String retVal = null;
try {
retVal = new URL(url).getPath();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return retVal;
}
}
What lines could I change to enforce a UTF-8 encoding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现编码不是UTF-8,而是ISO_8859_1。我将 ISO_8859_1 编码的输入流转换为 UTF-8,现在所有内容都按应有的方式显示。
I found out that the encoding wasn't UTF-8, but ISO_8859_1. I converted the ISO_8859_1-encoded inputstream to UTF-8, and now everything is displayed the way it should.
老问题,但如果我可以帮助任何人,我会发布我的解决方案。
类似的问题。我搜索了服务器日志,发现我的请求是 ISO-8859-1 编码的,因此服务器响应也具有此字符集。我的解决方案是覆盖 ksoap API。在 HttpTransportSE 类中,您提出问题,在方法调用中,我更改了该行:
我希望我的请求是 UTF-8 编码的,所以我添加:
现在,我发送一个 UTF-8 请求,并得到一个 UTF-8回复。
希望这有帮助。
Old question but if I could help anyone I post my solution.
Similar issue. I searched the server logs and saw my request was ISO-8859-1-encoded, so the server response had this charset too. My solution was override ksoap API. In HttpTransportSE class you put in your question, in the method call, I changed the line:
I wanted my request was UTF-8-encoded, so I added:
Now, I send a UTF-8 request, and get an UTF-8 response.
Hope this helps.
当我尝试将中文字符发送到 php 服务器并且服务器得到 ??? 时,我遇到了类似的问题,
我发现这是 ksoap 的一个错误,请阅读在此处输入链接说明< /a> 详细信息。
该错误在 ksoap2.6 中已修复。
i have a similar issue when i am trying to send Chinese character to php server and the server get ???,
i found it's a bug of ksoap, please read enter link description here for detail.
this bug is fixed in ksoap2.6.