通过web获取数据

                   String myString = null;

                   try {

                            /* Define the URL we want to load data from. */

                            URL myURL = new URL("http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");

/* Open a connection to that URL. */

                            URLConnection ucon = myURL.openConnection();

                            /*

                             * Define InputStreams to read from the URLConnection.

                             */

                            InputStream is = ucon.getInputStream();

                            BufferedInputStream bis = new BufferedInputStream(is);

                            /*

                             * Read bytes to the Buffer until there is nothing more to read(-1).

                             */

                            ByteArrayBuffer baf = new ByteArrayBuffer(50);

                            int current = 0;

                            while ((current = bis.read()) != -1) {

                                     baf.append((byte) current);

                            }

                            /* Convert the Bytes read to a String. */

                            myString = new String(baf.toByteArray());

                   } catch (Exception e) {

                            /* On any Error we want to display it. */

                            myString = e.getMessage();

                   }

你可能感兴趣的:(通过web获取数据)