Page 55 - The Definitive Guide to Building Java Robots
P. 55

Preston_5564C02.fm  Page 36  Wednesday, September 14, 2005  5:42 AM



                 36     CHAPTER 2  ■  SERIAL COMMUNICATION



                                    URL myurl = new URL(urlString);
                                    log(urlString);
                                    BufferedReader in = new BufferedReader(new InputStreamReader(
                                            myurl.openStream()));
                                    String str = null;
                                    while ((str = in.readLine()) != null) {
                                        // str is one line of text; readLine() strips the newline
                                        // character(s)
                                        if (str != null) {
                                            out = str;
                                        }
                                    }
                                    in.close();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                out = out.trim();
                                log("WebClient: out=" + out);
                                return out;
                            }

                            private String byteArrayToString(byte[] b) {
                                String s = "";
                                for (int x = 0; x < b.length; x++) {
                                    s = s + b[x] + ",";
                                }
                                s = s.substring(0, s.length() - 1);
                                return s;
                            }
                            private void log(String s) {
                                Date d = new Date();
                                String dt = formatter.format(d);
                                System.out.println(dt + " *** " + s);
                            }


                            public int getTimeout() {
                                return timeout;
                            }


                            public void setTimeout(int timeout) {
                                this.timeout = timeout;
                            }


                            public static void main(String[] args) {
                            }

                        }
   50   51   52   53   54   55   56   57   58   59   60