2012-03-24 27 views
36

Mã được dán dưới đây được lấy từ tài liệu java trên HttpURLConnection.Cách đọc luồng đầu vào http

tôi nhận được lỗi sau:

readStream(in) 

như không có phương pháp như vậy.

Tôi thấy điều tương tự này trong Tổng quan Class cho URLConnection tại URLConnection.getInputStream()

đâu readStream? Đoạn mã được cung cấp dưới đây:

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

Trả lời

54

Hãy thử với mã này:

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

Vì lợi ích của hiệu quả, 'kết quả' cũng phải là một đối tượng StringBuffer. –

+7

Tôi khuyên bạn nên sử dụng StringBuilder thay vì StringBuffer vì bạn không cần thêm phí đồng bộ hóa bổ sung. –

+3

'StringBuilder' rất tốt để sử dụng với thao tác đơn luồng. 'StringBuffer' nên được sử dụng khi nhiều luồng đang đọc và ghi một hơi nước vào cùng một đối tượng! –

12

Dường như các tài liệu chỉ được sử dụng readStream() nghĩa:

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

Vì vậy, bạn nên thể viết phương pháp readStream() của riêng bạn mà không bất cứ điều gì bạn muốn làm với dữ liệu ở nơi đầu tiên.

3

Spring có một lớp util cho rằng:

import org.springframework.util.FileCopyUtils; 

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

thử mã này

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

Có an toàn để giả sử mã hóa là utf8 không? – Edd

1

một mã hoàn chỉnh cho việc đọc từ một webservice trong hai cách

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); 
    } 
} 
Các vấn đề liên quan