如何读取HTTP输入流 [英] How to read an http input stream

查看:120
本文介绍了如何读取HTTP输入流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code粘贴下面是从Java文档的<一个href="http://developer.android.com/reference/java/net/HttpURLConnection.html">HttpURLConnection.

我收到以下错误:

  readStream(中)
 

,因为没有这样的方法。

我看到同样的事情在 类概述的URLConnection在 <一href="http://developer.android.com/reference/java/net/URLConnection.html#getInputStream%28%29"><$c$c>URLConnection.getInputStream()

在哪里 readStream ?在code段提供如下:

 网​​址URL =新的URL(http://www.android.com/);
    HttpURLConnection的的URLConnection =(HttpURLConnection类)url.openConnection();
    尝试
    {
        InputStream的时间=新的BufferedInputStream(urlConnection.getInputStream());
        readStream(在); &LT; -----这样的方法
    }
    最后
    {
        urlConnection.disconnect();
    }
 

解决方案

试试这个code:

 的InputStream在= address.openStream();
的BufferedReader读卡器=新的BufferedReader(新的InputStreamReader(在));
StringBuilder的结果=新的StringBuilder();
串线;
而((行= reader.readLine())!= NULL){
    result.append(线);
}
的System.out.println(result.toString());
 

The code pasted below was taken from java docs on HttpURLConnection.

I get the following error:

readStream(in) 

as there is no such method.

I see this same thing in the Class Overview for URLConnection at URLConnection.getInputStream()

Where is readStream? The code snippet is provided below:

 URL url = new URL("http://www.android.com/");   
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();   
    try 
    {     
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());     
        readStream(in);  <-----NO SUCH METHOD
    }
    finally 
    {     
        urlConnection.disconnect();   
    } 

解决方案

Try with this code:

InputStream in = address.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null) {
    result.append(line);
}
System.out.println(result.toString());

这篇关于如何读取HTTP输入流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆