有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从http协议读取并放入字符串数组(Android/Java)

我的代码从http连接读取数据,并将数据放入ByteArrayOutputStream

http数据内容的第一行是更新日期/时间,然后是其他数据

从http url接收的数据示例:

2012-03-02 03:06:34
text1
text2
text3

我发现:

    InputStream content = response.getEntity().getContent();
    byte[] buffer = new byte[1024];
    int numRead = 0;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    while((numRead=content.read(buffer))!=-1){
        baos.write(buffer, 0, numRead);
    }
    content.close();
    String result = new String(baos.toByteArray());

如何使用第一行(“2012-03-02 03:06:34”),然后使用其他行

我将考虑使用一个字符串数组,并使用baos[0]获取第一行,使用

for (int i=1;i<baos.length;i++) {...}

我怎么能? 谢谢 我的英语很难看


共 (1) 个答案

  1. # 1 楼答案

    你所拥有的更多是在一个字节一个字节的水平上工作

    一次尝试一句话:

        InputStream is = response.getEntity().getContent();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        while((line = br.readLine()) != null)
        {
            //Do something with each line
        }