50,331
社区成员




package com.dmj.test;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class Testbilibili {
public static void main(String[] args) {
InputStream iis=null;
try {
URL url=new URL("http://www.bilibili.com/index/rank/all-7-33.json");
url.openConnection();
iis=url.openStream();
byte[] bs=new byte[1024];
int length=0;
StringBuffer sb=new StringBuffer();
while((length=iis.read(bs,0,bs.length))!=-1){
sb.append(new String(bs,0,length));
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
iis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
String value=new String(sb.toString().getBytes("Unicode"),"utf-8");
System.out.println(value);
httpurlconnection.setRequestProperty("Content-type", "text/html");
httpurlconnection.setRequestProperty("Accept-Charset", "utf-8");
httpurlconnection.setRequestProperty("contentType", "utf-8");
URL url = new URL("http://www.bilibili.com/index/rank/all-7-33.json");
URLConnection conn = url.openConnection();
conn.connect();
GZIPInputStream gzipIs = new GZIPInputStream(conn.getInputStream());
InputStreamReader isr = new InputStreamReader(gzipIs, "Utf-8");
StringBuilder sb = new StringBuilder();
char[] cbuf = new char[1024];
int len = 0;
while ((len = isr.read(cbuf)) != -1) {
sb.append(cbuf);
}
System.out.println(sb.toString());
gzipIs.close();
isr.close();
数据加加过gzip压缩打过来的,需要用gzip流去读。