我抓一个网站。中文乱码了,怎么解决?

js2java 发布于 2016/06/08 16:13
阅读 216
收藏 0
public String connctionURL_Params(String url, String method, String params) {
		StringBuffer bufferRes = new StringBuffer();
		url = url + params;
		try {
			URL realUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
			// 请求方式
			conn.setRequestMethod(method);
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(true);
            conn.setRequestProperty("Content-Type", "application/json");
			conn.connect();

			InputStream in = conn.getInputStream();
			BufferedReader read = new BufferedReader(new InputStreamReader(in,"UTF-8"));
			String valueString = null;
			while ((valueString = read.readLine()) != null) {
				bufferRes.append(valueString);
			}
			read.close();
			in.close();
			in = null;
			if (conn != null) {
				// 关闭连接
				conn.disconnect();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bufferRes.toString();
	}



加载中
0
刘柳
刘柳
用httpClient或者 jsoup,设置一下字符集
0
l
lc52520
conn.getInputStream();是ISO-8859-1吧,需要做转换才能变为utf-8
0
加州肥猫
加州肥猫
json的就这样,写出来就没事了
返回顶部
顶部