2012-03-24 62 views
36

下面粘貼的代碼取自HttpURLConnection上的java文檔。如何讀取http輸入流

我得到以下錯誤:

readStream(in) 

,因爲沒有這樣的方法。

我看到 URLConnection.getInputStream()

哪裏是readStream在 類概述的URLConnection同樣的事情?代碼段提供如下:

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

回答

54

嘗試使用此代碼:

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

爲了提高效率,'result'也應該是一個StringBuffer對象。 – 2013-04-17 19:00:29

+7

我建議使用StringBuilder而不是StringBuffer,因爲你不需要額外的同步開銷。 – 2014-10-01 08:11:36

+3

'StringBuilder'很適合單線程操作。當多個線程正在讀取和寫入蒸汽到同一個對象時,應該使用'StringBuffer'! – 2015-06-18 10:55:56

12

它看起來像文檔只是用readStream()意味着:

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

所以您應該寫自己的readStream()方法,做任何你想和做數據在第一位。

3

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

試試這個代碼

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

假設編碼是utf8是否安全? – Edd 2016-07-18 14:19:14

1

完整的代碼爲從Web服務讀兩種方式

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