在我的 android 应用程序中,我使用套接字并获取 XML 作为响应。我得到的 XML 如下:
<response>
<result>
<id>90</id>
<name>J�nna</name>
</result>
</response>��
以下是获取响应的代码:
PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()) ) ;
BufferedReader reader = new BufferedReader( new InputStreamReader( socket.getInputStream(), "UTF-8") );
writer.write(sMessage);
writer.flush();
StringBuilder sb = new StringBuilder();
boolean dataStreamStarted = false;
int sleepCounter = 0;
int sleepMaxCounter = 50;
int sleepMaxDataCounter = 3;
int sleepTime = 50;
long startTime = System.currentTimeMillis();
while( true )
{
if ( reader.ready() )
{
sb.append( (char) reader.read() );
dataStreamStarted = true;
}
else
{
Thread.sleep( sleepTime );
if ( dataStreamStarted && (sleepCounter >= sleepMaxDataCounter) )
{
break;
}
else
{
if (sleepCounter >= sleepMaxCounter)
{
break;
}
}
sleepCounter++;
}
}
long endTime = System.currentTimeMillis();
writer.close();
reader.close();
socket.close();
response = sb.toString();
我尝试使用以下代码解码此字符串:
String str = URLDecoder.decode(response, "UTF-8");
但它没有任何区别。字符串看起来仍然一样。由于这些字符,我无法解析这个字符串。知道我该如何解决吗?我搜索了很多,我找到的任何解决方案都不起作用。提前致谢。