List of usage examples for java.net HttpURLConnection getErrorStream
public InputStream getErrorStream()
From source file:com.cloudant.http.interceptors.Replay429Interceptor.java
@Override public HttpConnectionInterceptorContext interceptResponse(HttpConnectionInterceptorContext context) { // Get or init the stored context for this interceptor try {//from w w w.j a v a 2 s. c o m HttpURLConnection urlConnection = context.connection.getConnection(); int code = urlConnection.getResponseCode(); // We only want to take action on a 429 response if (code != 429) { return context; } // We received a 429 // Get the counter from the request context state AtomicInteger attemptCounter = context.getState(this, ATTEMPT, AtomicInteger.class); // If there was no counter yet, then this is the first 429 received for this request if (attemptCounter == null) { context.setState(this, ATTEMPT, (attemptCounter = new AtomicInteger())); } // Get the current value, and then increment for the next time round int attempt = attemptCounter.getAndIncrement(); // Check if we have remaining replays if (attempt < numberOfReplays && context.connection.getNumberOfRetriesRemaining() > 0) { // Calculate the backoff time, 2^n * initial sleep long sleepTime = initialSleep * Math.round(Math.pow(2, attempt)); // If the response includes a Retry-After then that is when we will retry, otherwise // we use the doubling sleep String retryAfter = preferRetryAfter ? urlConnection.getHeaderField("Retry-After") : null; if (retryAfter != null) { // See https://tools.ietf.org/html/rfc6585#section-4 // Whilst not specified for 429 for 3xx or 503 responses the Retry-After header // is expressed as an integer number of seconds or a date in one of the 3 HTTP // date formats https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3 // Cloudant servers should give us an integer number of seconds, so don't worry // about parsing dates for now. try { sleepTime = Long.parseLong(retryAfter) * 1000; if (sleepTime > RETRY_AFTER_CAP) { sleepTime = RETRY_AFTER_CAP; logger.severe("Server specified Retry-After value in excess of one " + "hour, capping retry."); } } catch (NumberFormatException nfe) { logger.warning( "Invalid Retry-After value from server falling back to " + "default backoff."); } } // Read the reasons and log a warning InputStream errorStream = urlConnection.getErrorStream(); try { String errorString = IOUtils.toString(errorStream, "UTF-8"); logger.warning(errorString + " will retry in " + sleepTime + " ms"); } finally { errorStream.close(); } logger.fine("Too many requests backing off for " + sleepTime + " ms."); // Sleep the thread for the appropriate backoff time try { TimeUnit.MILLISECONDS.sleep(sleepTime); } catch (InterruptedException e) { logger.fine("Interrupted during 429 backoff wait."); // If the thread was interrupted we'll just continue and try again a bit earlier // than planned. } // Get ready to replay the request after the backoff time context.replayRequest = true; return context; } else { return context; } } catch (IOException e) { throw new HttpConnectionInterceptorException(e); } }
From source file:com.canappi.connector.yp.yhere.CouponView.java
public ArrayList<Element> getCouponsByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {//from w w w . ja v a2 s . c o m URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/coupons?format=xml&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/coupons?format=xml&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }
From source file:com.canappi.connector.yp.yhere.GameView.java
public ArrayList<Element> searchGamesByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {//from w w w .ja va2 s. co m URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=games&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=games&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }
From source file:com.canappi.connector.yp.yhere.BakeryView.java
public ArrayList<Element> searchBakeriesByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {//from w w w .j a v a2 s.co m URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=bakery&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=bakery&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }
From source file:com.canappi.connector.yp.yhere.GroceryView.java
public ArrayList<Element> searchGroceryStoresByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {/*from w w w . j a va 2s .c om*/ URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=grocery&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=grocery&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }
From source file:com.canappi.connector.yp.yhere.TheaterView.java
public ArrayList<Element> searchTeathersByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {/*ww w . j a v a 2s. c om*/ URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=theater&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=theater&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }
From source file:com.canappi.connector.yp.yhere.LubeView.java
public ArrayList<Element> searchOilChangeByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {//from w w w .java 2s.c o m URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=oil+change&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=oil+change&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }
From source file:com.canappi.connector.yp.yhere.RestaurantView.java
public ArrayList<Element> searchRestaurantsByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {/*from w ww .j av a2s. com*/ URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=restaurant&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=restaurant&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }
From source file:com.canappi.connector.yp.yhere.GasStationView.java
public ArrayList<Element> searchGasStationByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {//from w w w.j a v a2 s. c om URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=gas+station&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=gas+station&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }
From source file:com.canappi.connector.yp.yhere.RepairView.java
public ArrayList<Element> searchRepairByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {/*from w w w .j a v a 2 s . c om*/ URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=auto+repair&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/search?format=xml&sort=distance&radius=5&term=auto+repair&listingcount=10&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }