如何读取httpinputstream

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

我得到以下错误:

readStream(in) 

因为没有这样的方法。

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

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

试试这个代码:

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

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

好的,我们已经告诉你如何获得InputStream,现在你的代码进入readStream()

所以你应该编写你自己的readStream()方法,它首先做你想做的任何事情。

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

试试这个代码

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

一个完整的代码以两种方式从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); } }