Android URLConnection returns ASCII 小数?
Android URLConnection returns ASCII decimals?
我正在构建一个 Android 应用程序,它使用 URLConnection 来抓取网页内容,但出于某种原因,它会将实际撇号 (') 等符号转换为 ASCII 十进制值 ('
)
示例:Let's go to the party
变为 Let's go to the party
。
我已经尝试将 InputStream 字符集设置为 ASCII,但这没有帮助。
代码:
String bodyHtml;
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "ASCII");
int numCharsRead;
char[] charArray = new char[1024];
StringBuilder sb = new StringBuilder();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
/*StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}*/
bodyHtml = sb.toString();
bodyHtml = URLDecoder.decode(bodyHtml);
您需要将收到的字符串转换为 html 然后再转换回字符串。 Html.fromHtml(value) 会将收到的值转换为 html。调用 .toString() 将 return 你的字符串(没有任何 html 标签)
// import this package
import android.text.Html;
收到来自 url 的内容后..您可以将该内容转换为可读形式..
String value ="Let's go to the party";
String formattedValue = Html.fromHtml(value).toString().trim();
我正在构建一个 Android 应用程序,它使用 URLConnection 来抓取网页内容,但出于某种原因,它会将实际撇号 (') 等符号转换为 ASCII 十进制值 ('
)
示例:Let's go to the party
变为 Let's go to the party
。
我已经尝试将 InputStream 字符集设置为 ASCII,但这没有帮助。
代码:
String bodyHtml;
URL url = new URL(webPage);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream is = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "ASCII");
int numCharsRead;
char[] charArray = new char[1024];
StringBuilder sb = new StringBuilder();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
/*StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}*/
bodyHtml = sb.toString();
bodyHtml = URLDecoder.decode(bodyHtml);
您需要将收到的字符串转换为 html 然后再转换回字符串。 Html.fromHtml(value) 会将收到的值转换为 html。调用 .toString() 将 return 你的字符串(没有任何 html 标签)
// import this package
import android.text.Html;
收到来自 url 的内容后..您可以将该内容转换为可读形式..
String value ="Let's go to the party";
String formattedValue = Html.fromHtml(value).toString().trim();