java如何从InputStream对象读取特定数据?
当我使用HttpUrlConnection对象建立连接时,如何从InputStream对象读取特定数据。我想读取和发布数据,例如,我想读取特定的标签数据,比如等等。就像我想发布的一样
public class Test {
public static void main(String[] args) throws Exception {
HttpsURLConnection con = (HttpsURLConnection) new URL("some URL ...")
.openConnection();
// reading data from connection
InputStream in = new BufferedInputStream(con.getInputStream());
System.out.println(readStream(in));
}
private static String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
}
请帮忙
# 1 楼答案
试试这个,我在我的案例中得到了JSONObject:
# 2 楼答案
使用jsouphttp://jsoup.org 它有工具来处理你想用html做的所有事情