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

时间:2014-11-15 11:23:10

标签: java android

如何从InputStream对象读取特定数据,而我正在使用HttpUrlConnection对象进行连接。我想读取和POST数据,例如,我想读取特定的Tag数据,等等。同样聪明我也想发布。

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

}

请帮助..

2 个答案:

答案 0 :(得分:0)

试试这个我在我的案例中得到了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();
            }

答案 1 :(得分:0)

使用jsoup http://jsoup.org 它有工具来处理你想用html做的所有事情。