關於java.io.IOException問題


  這個是一個很莫名的問題,通常讓人很難發現。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()竟會關閉流。。。。  正確做法保留一個即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM