我正在编写代码来处理来自维基百科的文本,并且遇到了破折号乱码的问题。我以前没有使用过破折号或其他非标准字符(对我来说是非标准的字符,不会出现在我的键盘上;),所以我不确定在哪里指向我的手指做错了。这是正在发生的事情,以及代码片段......
我向 Wikipedia 发送请求(我使用 Apache HttpComponents 客户端 API 与 Wikipedia 进行通信)以获取文章的内容并将其保存在字符串中:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet queryRequest = new HttpGet(query); // query is the URL for retrieving the article contents.
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(queryRequest, responseHandler);
此时,如果我要向 System.out 发送“responseBody”,则短划线会在我的 Eclipse 控制台中显示为“?”。这可能只是一个 Eclipse 控制台显示问题,所以我将继续。
我处理文本,忽略短划线,然后将文本发送回维基百科。
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("text", content); // content is a String with the article text
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams, "UTF-8");
HttpPost queryRequest = new HttpPost(url); // url is the basic URL for the Wikipedia api
queryRequest.setEntity(entity);
queryRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = client.execute(queryRequest, responseHandler);
当现在上传到维基百科的文本显示在网络浏览器中时,之前的破折号现在显示为“?” 在一个盒子里(未知字符?)。因此,在某处我无意中更改或错误编码了破折号,但我不确定确切的位置。
有人可以指出我正确的方向吗?