2012-03-24 21 views
36

नीचे चिपकाया गया कोड जावा डॉक्स से HttpURLConnection पर लिया गया था।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

इस कोड के साथ प्रयास करें:

import org.springframework.util.FileCopyUtils; 

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

दक्षता के लिए, 'परिणाम' भी एक स्ट्रिंगबफर ऑब्जेक्ट होना चाहिए। –

+7

मैं स्ट्रिंगबफर के बजाय स्ट्रिंगबिल्डर का उपयोग करने का सुझाव दूंगा क्योंकि आपको अतिरिक्त सिंक्रनाइज़ेशन ओवरहेड की आवश्यकता नहीं है। –

+3

'स्ट्रिंगबिल्डर' एकल थ्रेडेड ऑपरेशन के साथ उपयोग करने के लिए अच्छा है। 'स्ट्रिंगबफर' का उपयोग तब किया जाना चाहिए जब एकाधिक थ्रेड पढ़ रहे हों और उसी ऑब्जेक्ट में स्टीम लिख रहे हों! –

12

ऐसा लगता है कि प्रलेखन सिर्फ readStream() उपयोग कर रहा है मतलब करने के लिए:

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

तो आप या तो अपने स्वयं के readStream() विधि लिखना चाहिए जो करता है जो कुछ भी आप के साथ करना चाहता था डेटा पहले स्थान पर।

3

स्प्रिंग कि के लिए एक util वर्ग है दो तरीके

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

1

एक पूरा कोड का प्रयास करें:

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()); 
संबंधित मुद्दे