This is an inexplicable problem, which is usually hard to find. Java. Io. ioexception: stream closed indicates that the stream is closed. Oh, my God, I didn't close it. The younger brother has encountered this problem:
Public class locationservice {/*** use Google map to obtain the current city through the current latitude and longitude of the user */static final string google_maps_api_key = "Hangzhou"; private context mcontext; private locationmanager; private string city = "not found"; private string address = "not found"; Public locationservice (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. getl Astknownlocation (locationmanager. network_provider); Return currentlocation;}/*** parse the location, the you cocould call getcity () or getaddress () * @ Param location */Public void reversegeocode (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 & longpolling 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. getlongpolling () + "& Output = xml & OE = utf8 & sensor = true & Key =" + google_maps_api_key); connection = NULL; // set up the initial connection = (httpurlconnection) serveraddress. openconnection (); connection. setrequestmethod ("get"); connection. setdooutput (true); connection. setreadtimeout (10000); connection. connect (); tr Y {// 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 = new saxhandler (); try {reader = factory. newsaxparser (). getxmlre Ader (); 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 ();}}
This error occurs after a run: Java. Io. ioexception: stream closed. After a long time, I found that I called httpurlconnction. getinputstream () twice .... Unexpectedly, if httpurlconnection is called twice, getinputsream () will actually close the stream .... Keep one record for the correct method.