How to read an http input stream

后端 未结 5 563
孤独总比滥情好
孤独总比滥情好 2020-12-01 07:24

The code pasted below was taken from java docs on HttpURLConnection.

I get the following error:

readStream(in) 

as there is no suc

相关标签:
5条回答
  • 2020-12-01 07:45

    a complete code for reading from a webservice in two ways

    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);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 07:52

    Try with this code:

    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());
    
    0 讨论(0)
  • 2020-12-01 07:55

    Spring has an util class for that:

    import org.springframework.util.FileCopyUtils;
    
    InputStream is = connection.getInputStream();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    FileCopyUtils.copy(is, bos);
    String data = new String(bos.toByteArray());
    
    0 讨论(0)
  • 2020-12-01 07:57

    try this code

    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 讨论(0)
  • 2020-12-01 08:06

    It looks like the documentation is just using readStream() to mean:

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

    So you should either write your own readStream() method which does whatever you wanted to do with the data in the first place.

    0 讨论(0)
提交回复
热议问题