關於java.io.IOException問題

來源:互聯網
上載者:User

  這個是一個很莫名的問題,通常讓人很難發現。java.io.IOException: stream closed   意思是說流關閉. 天啊,我沒有關閉它啊。小弟就遇到過這個問題:

public class LocationService {    /**     * 藉助Google MAP 通過使用者當前經緯度 獲得使用者當前城市     */    static final String GOOGLE_MAPS_API_KEY = "0835yI5X5qhWP3POFjEeQhZwN1xgAt9rmv5688w";    private Context mContext;    private LocationManager locationManager;        private String city = "not found";    private String address = "not found";    public LocationService(Context context) {        mContext = context;                    }    public String getCity() {        return city;    }        public String getAddress(){        return address;    }    /**     * get the current-local Location     * @return     */    private Location getLocation(){        Location currentLocation;        this.locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);        currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);        if(currentLocation == null)            currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);        return currentLocation;    }        /**     * parse the location, the you could call getCity() or getAddress()     * @param location     */    public void reverseGeocode(Location location) {        // http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&oe=utf8&sensor=true_or_false&key=your_api_key        HttpURLConnection connection = null;        URL serverAddress = null;        if(location==null)            location = getLocation();        try {            // build the URL using the latitude & longitude you want to lookup            // NOTE: I chose XML return format here but you can choose something            serverAddress = new URL("http://maps.google.com/maps/geo?q="                    + Double.toString(location.getLatitude()) + ","                    + Double.toString(location.getLongitude())                    + "&output=xml&oe=utf8&sensor=true&key="                    + GOOGLE_MAPS_API_KEY);            connection = null;            // Set up the initial connection            connection = (HttpURLConnection) serverAddress.openConnection();            connection.setRequestMethod("GET");            connection.setDoOutput(true);            connection.setReadTimeout(10000);            connection.connect();            try {                //InputStreamReader isr = new InputStreamReader(connection.getInputStream());                String result = read(connection.getInputStream());                System.out.println("result: " + result);                SAXParserFactory factory=SAXParserFactory.newInstance();                InputSource is = new InputSource(connection.getInputStream());                XMLReader reader;                SAXHandler saxHandler = new SAXHandler();                try {                    reader = factory.newSAXParser().getXMLReader();                    reader.setContentHandler(saxHandler);                     is.setEncoding("utf-8");                    reader.parse(is);                 } catch (Exception e) {                    e.printStackTrace();                }                                 city = saxHandler.getCity();                address = saxHandler.getAddress();                System.out.println("address: "+saxHandler.getAddress());                System.out.println("city: "+saxHandler.getCity());            } catch (Exception ex) {                ex.printStackTrace();            }        } catch (Exception ex) {            ex.printStackTrace();            System.out.println("GetCity.reverseGeocode()" + ex);        }    }        private  String read(InputStream in) throws IOException {        StringBuilder sb = new StringBuilder();        BufferedReader r = new BufferedReader(new InputStreamReader(in), 1024);        for (String line = r.readLine(); line != null; line = r.readLine()) {            sb.append(line);        }        in.close();        return sb.toString();    }}

  一下運行就會出現這個錯誤:java.io.IOException : stream closed  .   搞了好久才發現我httpurlconnction.getInputStream()我上面調用了兩次。。。。  真是想不到對於HttpUrlConnection調用兩次getInputSream()竟會關閉流。。。。  正確做法保留一個即可

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.