Java InputStream读取网络响应Response数据的方法

Java InputStream读取数据问题

原理讲解 


1. 关于InputStream.read()
     在从数据流里读取数据时,为图简单,经常用InputStream.read()方法。这个方法是从流里每次只读取读取一个字节,效率会非常低。     更好的方法是用InputStream.read(byte[] b)或者InputStream.read(byte[] b,int off,int len)方法,一次读取多个字节。
 

2. 关于InputStream类的available()方法
    要一次读取多个字节时,经常用到InputStream.available()方法,这个方法可以在读写操作前先得知数据流里有多少个字节可以读取。需要注意的是,如果这个方法用在从本
地文件读取数据时,一般不会遇到问题,但如果是用于网络操作,就经常会遇到一些麻烦。比如,Socket通讯时,对方明明发来了1000个字节,但是自己的程序调用available()方法却只得到900,或者100,甚至是0,感觉有点莫名其妙,怎么也找不到原因。其实,这是因为网络通讯往往是间断性的,一串字节往往分几批进行发送。本地程序调用available()方法有时得到0,这可能是对方还没有响应,也可能是对方已经响应了,但是数据还没有送达本地。对方发送了1000个字节给你,也许分成3批到达,这你就要调用3次available()方法才能将数据总数全部得到。
      如果这样写代码:

<span style="font-family:SimHei;font-size:16px;">  int count = in.available();  
  byte[] b = new byte[count];  
  in.read(b);</span> 

   在进行网络操作时往往出错,因为你调用available()方法时,对发发送的数据可能还没有到达,你得到的count是0。
         需要改成这样:

int count = 0;  
 while (count == 0) {  
  //count = in.available();  
  
  count=response.getEntity().getContentLength();//(HttpResponse response)  
 }  
 byte[] b = new byte[count];  
 in.read(b);

3. 关于InputStream.read(byte[] b)和InputStream.read(byte[] b,int off,int len)这两个方法都是用来从流里读取多个字节的,有经验的程序员就会发现,这两个方法经常 读取不到自己想要读取的个数的字节。比如第一个方法,程序员往往希望程序能读取到b.length个字节,而实际情况是,系统往往读取不了这么多。仔细阅读Java的API说明就发现了,这个方法 并不保证能读取这么多个字节,它只能保证最多读取这么多个字节(最少1个)。因此,如果要让程序读取count个字节,最好用以下代码:

byte[] bytes = new byte[count];  
  int readCount = 0; // 已经成功读取的字节的个数  
  while (readCount < count) {  
   readCount += in.read(bytes, readCount, count - readCount);  
  }  

用这段代码可以保证读取count个字节,除非中途遇到IO异常或者到了数据流的结尾(EOFException)

代码分享

下面分享我自己写的测试代码片段,供大家参考:

/**
     * <b>获取指定的URL返回的数据信息</b>
     * @param <font color="#efac10"><a href="http://www.baidu.com">_url:指定的URL</a></font>
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public String getReponse(String _url) throws ClientProtocolException, IOException
    {
        String readContent=null;
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(SinaJsonTest.SinaUrl);
        System.out.println("0.Send the URL to Sina Sever....");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();

        System.out.println("1.Get Response Status: " + response.getStatusLine());
        if (entity != null) {
            System.out.println("  Get ResponseContentEncoding():"+entity.getContentEncoding());
            System.out.println("  Content Length():"+entity.getContentLength());
            //getResponse
            InputStream in=entity.getContent();
            int count = 0;
            while (count == 0) {
             count = Integer.parseInt(""+entity.getContentLength());//in.available();
            }
            byte[] bytes = new byte[count];
            int readCount = 0; // 已经成功读取的字节的个数
            while (readCount <= count) {
             if(readCount == count)break;
             readCount += in.read(bytes, readCount, count - readCount);
            }
            
            //转换成字符串
            readContent= new String(bytes, 0, readCount, "UTF-8"); // convert to string using bytes

            System.out.println("2.Get Response Content():
"+readContent);
        }
        return readContent;
    }

转载自:http://blog.csdn.net/hurryjiang/article/details/6688247

原文地址:https://www.cnblogs.com/itliucheng/p/4987510.html