2012-03-24 19 views
36

以下のコードは、java docsのHttpURLConnectionに貼り付けられています。そのようなメソッドが存在しないようhttp入力ストリームの読み方

readStream(in) 

は、私は次のエラーを取得します。

私はreadStreamある URLConnection.getInputStream()

で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オブジェクトでなければなりません。 –

+7

余分な同期オーバーヘッドは必要ないので、StringBuilderの代わりにStringBuilderを使用することをお勧めします。 –

+3

'StringBuilder'は、シングルスレッド操作で使うのに適しています。 'StringBuffer'は、複数のスレッドが同じオブジェクトに蒸気を読み書きしているときに使用する必要があります。 –

12

ドキュメントはちょうど意味するreadStream()を使用しているように見えます:

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

をだからあなたはあなたがやってみたかったものは何でもないあなた自身のreadStream()方法を記述しなければならないのいずれか最初の場所のデータ。

3

春にはそのための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

1

でWebサービスから読み取るための完全なコードを試してみてください2つの方法

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); 
    } 
} 
関連する問題