Java 如何读取http输入流

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9856195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 12:22:34  来源:igfitidea点击:

How to read an http input stream

javaandroid

提问by Dean Blakely

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

下面粘贴的代码取自HttpURLConnection上的 java 文档。

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()

我在 URLConnection 的类概述中看到了同样的事情 URLConnection.getInputStream()

Where is readStream? The code snippet is provided below:

在哪里readStream?下面提供了代码片段:

 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();   
    } 

采纳答案by Emran

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());

回答by ulmangt

It looks like the documentation is just using readStream()to mean:

看起来文档只是readStream()用来表示:

Ok, we've shown you how to get the InputStream, now your code goes in readStream()

好的,我们已经向您展示了如何获取 InputStream,现在您的代码进入 readStream()

So you should either write your own readStream()method which does whatever you wanted to do with the data in the first place.

因此,您应该编写自己的readStream()方法,该方法首先可以对数据执行任何操作。

回答by Eugene Retunsky

Spring has an util class for that:

Spring 有一个 util 类:

import org.springframework.util.FileCopyUtils;

InputStream is = connection.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
FileCopyUtils.copy(is, bos);
String data = new String(bos.toByteArray());

回答by anu

try this code

试试这个代码

String data = "";
InputStream iStream = httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream, "utf8"));
StringBuffer sb = new StringBuffer();
String line = "";

while ((line = br.readLine()) != null) {
    sb.append(line);
}

data = sb.toString();
System.out.println(data);

回答by frans

a complete code for reading from a webservice in two ways

两种方式从webservice读取的完整代码

public void buttonclick(View view) {
    // the name of your webservice where reactance is your method
    new GetMethodDemo().execute("http://wervicename.nl/service.asmx/reactance");
}

public class GetMethodDemo extends AsyncTask<String, Void, String> {
    //see also: 
    // https://developer.android.com/reference/java/net/HttpURLConnection.html
    //writing to see:    https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html
    String server_response;
    @Override
    protected String doInBackground(String... strings) {
        URL url;
        HttpURLConnection urlConnection = null;
        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            int responseCode = urlConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                server_response = readStream(urlConnection.getInputStream());
                Log.v("CatalogClient", server_response);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            url = new URL(strings[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    urlConnection.getInputStream()));
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                System.out.println(inputLine);
            in.close();
            Log.v("bufferv ", server_response);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Log.e("Response", "" + server_response);
   //assume there is a field with id editText
        EditText editText = (EditText) findViewById(R.id.editText);
        editText.setText(server_response);
    }
}