2012-03-24 13 views
36

Kod wklejony poniżej został pobrany z dokumentacji java pod numerem HttpURLConnection.Sposób odczytywania strumienia wejściowego http

pojawia się następujący błąd:

readStream(in) 

jak nie ma takiej metody.

widzę to samo w Przegląd klasy dla URLConnection w URLConnection.getInputStream()

Gdzie jest readStream? Fragment kodu znajduje się poniżej:

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

Odpowiedz

54

Spróbuj z tym kodem:

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

Dla efektywności, 'result' powinien być również obiektem StringBuffer. –

+7

Proponuję użyć StringBuilder zamiast StringBuffer, ponieważ nie potrzebujesz dodatkowego obciążenia synchronizacji. –

+3

'StringBuilder' jest dobry w użyciu z pojedynczą operacją gwintowania. 'StringBuffer' powinien być użyty, gdy wiele wątków czyta i zapisuje parę do tego samego obiektu! –

12

Wygląda na to dokumentacja jest tylko przy użyciu readStream() oznaczać:

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

Więc powinien albo napisać własną metodę readStream() który robi cokolwiek chciał zrobić z dane w pierwszej kolejności.

3

Wiosna ma klasę util na to:

import org.springframework.util.FileCopyUtils; 

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

wypróbować ten kod

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

Czy można bezpiecznie założyć, że kodowanie to utf8? – Edd

1

kompletny kod do odczytu z usługa w dwa sposoby:

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); 
    } 
} 
Powiązane problemy