简体   繁体   English

如何从InputStream对象读取特定数据?

[英]How to Read Specific data from to InputStream object?

How to read Specific data from InputStream object, While i am using HttpUrlConnection object to make connection. 当我使用HttpUrlConnection对象建立连接时,如何从InputStream对象读取特定数据。 I want to READ and POST data ,for example , i want to read specific Tag data , like and so on. 我想读取和发布数据,例如,我想读取特定的标签数据,诸如此类。 like wise i want to Post also. 就像明智的我也想发布。

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 "";
    }
}

} }

please help.. 请帮忙..

Try this I was getting JSONObject in my case : 试试这个我在我的情况下得到JSONObject:

static InputStream is = null;


            try {

                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);

                StringBuilder sb = new StringBuilder();

                String line = null;

                while ((line = reader.readLine()) != null) {

                    sb.append(line + "\n");

                }

                is.close();

                json = sb.toString();

            } catch (Exception e) {

                e.printStackTrace();
            }

使用jsoup http://jsoup.org它具有处理html所需的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM